ScrewTurn.Wiki.ImageEditor.ResizeImage C# (CSharp) Method

ResizeImage() private method

private ResizeImage ( ) : void
return void
        private void ResizeImage()
        {
            SetProvider();
            SetInputData();

            // Contains the image bytes
            MemoryStream ms = new MemoryStream(1048576);

            // Load from provider
            if(string.IsNullOrEmpty(page)) {
                if(!provider.RetrieveFile(file, ms, false)) {
                    Response.StatusCode = 404;
                    Response.Write("File not found.");
                    return;
                }
            }
            else {
                PageInfo info = Pages.FindPage(page);
                if(info == null) {
                    Response.StatusCode = 404;
                    Response.WriteFile("Page not found.");
                    return;
                }

                if(!provider.RetrievePageAttachment(info, file, ms, false)) {
                    Response.StatusCode = 404;
                    Response.WriteFile("File not found.");
                    return;
                }
            }

            // Setup new file name and image format
            if(!Page.IsPostBack) {
                string ext = Path.GetExtension(file);
                txtNewName.Text = Path.GetFileNameWithoutExtension(file) + "-2" + ext;
                switch(ext.ToLowerInvariant()) {
                    case ".jpg":
                    case ".jpeg":
                        rdoPng.Checked = false;
                        rdoJpegHigh.Checked = true;
                        rdoJpegMedium.Checked = false;
                        break;
                    default:
                        rdoPng.Checked = true;
                        rdoJpegHigh.Checked = false;
                        rdoJpegMedium.Checked = false;
                        break;
                }

            }

            ms.Seek(0, SeekOrigin.Begin);

            // Load the source image
            System.Drawing.Image source = System.Drawing.Image.FromStream(ms);

            lblCurrentSize.Text = string.Format("{0}x{1}", source.Width, source.Height);
            if(!Page.IsPostBack) {
                txtWidth.Text = source.Width.ToString();
                txtHeight.Text = source.Height.ToString();
            }

            // Put dimension in script
            lblDimensions.Text = "<script type=\"text/javascript\"><!--\r\nvar width = " +
                source.Width.ToString() +
                ";\r\nvar height = " + source.Height.ToString() +
                ";\r\n// -->\r\n</script>";

            int resultWidth = source.Width;
            int resultHeight = source.Height;

            if(rdoPercentage.Checked) {
                // Resize by percentage
                int dim = 100;
                if(string.IsNullOrEmpty(txtPercentage.Text)) dim = 100;
                else {
                    try {
                        dim = int.Parse(txtPercentage.Text);
                    }
                    catch(FormatException) { }
                }

                // Possible final preview dimensions
                resultWidth = source.Width * dim / 100;
                resultHeight = source.Height * dim / 100;
            }
            else {
                // Resize by pixel
                if(!string.IsNullOrEmpty(txtWidth.Text) && !string.IsNullOrEmpty(txtHeight.Text)) {
                    try {
                        resultWidth = int.Parse(txtWidth.Text);
                        resultHeight = int.Parse(txtHeight.Text);
                    }
                    catch(FormatException) { }
                }
            }

            int rotation = GetSelectedRotation();

            // Draw preview
            if(resultWidth > 290 || resultHeight > 290) {
                int previewWidth = resultWidth;
                int previewHeight = resultHeight;

                // Max preview dimension 290x290
                if(resultWidth > resultHeight) {
                    previewWidth = 290;
                    previewHeight = (int)((float)290 / (float)resultWidth * (float)resultHeight);
                    lblScale.Text = string.Format("{0:N0}", (float)290 / (float)resultWidth * 100);
                }
                else {
                    previewHeight = 290;
                    previewWidth = (int)((float)290 / (float)resultHeight * (float)resultWidth);
                    lblScale.Text = string.Format("{0:N0}", (float)290 / (float)resultHeight * 100);
                }
                imgPreview.ImageUrl = "Thumb.aspx?File=" + Request["File"] +
                    @"&Size=imgeditprev&Width=" + previewWidth + @"&Height=" + previewHeight +
                    @"&Page=" + (string.IsNullOrEmpty(page) ? "" : page) +
                    (!rdoNoRotation.Checked ? ("&Rot=" + rotation.ToString()) : "");
            }
            else {
                lblScale.Text = "100";
                imgPreview.ImageUrl = "Thumb.aspx?File=" + Request["File"] +
                    @"&Size=imgeditprev&Width=" + resultWidth + @"&Height=" + resultHeight +
                    @"&Page=" + (string.IsNullOrEmpty(page) ? "" : page) +
                    (!rdoNoRotation.Checked ? ("&Rot=" + rotation.ToString()) : "");
            }

            // Destination bitmap
            Bitmap result = new Bitmap(
                rotation != 90 && rotation != 270 ? resultWidth : resultHeight,
                rotation != 90 && rotation != 270 ? resultHeight : resultWidth,
                System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            // Get Graphics object for destination bitmap
            Graphics g = Graphics.FromImage(result);
            g.Clear(Color.Transparent);

            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;

            g.TranslateTransform(result.Width / 2, result.Height / 2);
            g.RotateTransform(rotation);
            g.TranslateTransform(-result.Width / 2, -result.Height / 2);

            // Draw bitmap
            g.DrawImage(source, GetImageRectangle(result.Width, result.Height,
                rotation != 90 && rotation != 270 ? source.Width : source.Height,
                rotation != 90 && rotation != 270 ? source.Height : source.Width,
                rotation == 90 || rotation == 270));

            // Prepare encoder parameters
            string format = GetCurrentFormat();
            resultMemStream = new MemoryStream(1048576);

            // Only JPEG and PNG images are editable
            if(format == "image/jpeg") {
                EncoderParameters encoderParams = new EncoderParameters(1);
                encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, rdoJpegHigh.Checked ? 100L : 60L);

                result.Save(resultMemStream, GetEncoderInfo(format), encoderParams);
            }
            else {
                result.Save(resultMemStream, ImageFormat.Png);
            }

            resultMemStream.Seek(0, SeekOrigin.Begin);

            // Dispose of source and result bitmaps
            source.Dispose();
            g.Dispose();
            result.Dispose();
        }