Deepika Thakur
Deepika Thakur

Reputation: 173

Crop image without distorting pixels in c#

The image gets distorted while cropping it into various sizes.
How do I do this without affecting the image quality? My current result is a distorted blurry image. Please help.

Here is my code:

var common = new Common();
string filesPath = HttpContext.Current.Server.MapPath(Const.directoryPath);
string imageUrl1 = UploadImageToAzure(1123, "\\Configurator\\_trunk\\Content\\TempImages\\eddaec5aa33e4b1593b304674a842874.jpeg, "eddaec5aa33e4b1593b304674a842874_260x190.jpeg", cloudstorage, containerName);
string cropimage260x190 = CropImagewithName(inputStream/*System.Io.Stream*/, 260, 190, cropedImageName);


public string CropImagewithName(Stream stream, int width, int height, string name)
        {
            int bmpW = 0;
            int bmpH = 0;
            string filePath = string.Empty;
            string imgName = string.Empty;
            try
            {
                {
                    bmpW = width;
                    bmpH = height;
                    int newWidth = bmpW;
                    int newHeight = bmpH;

                    imgName = name;
                    imgName = imgName.Replace("-", "");
                    filePath = Const.directoryPath + imgName;
                    this.upBmp = new Bitmap(stream);

                    this.newBmp = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                    this.newBmp.SetResolution(300, 300);

                    int upWidth = this.upBmp.Width;
                    int upHeight = this.upBmp.Height;
                    int newX = 0;
                    int newY = 0;
                    decimal reDuce;
                    if (upWidth > upHeight)
                    {
                        reDuce = Convert.ToDecimal(newWidth) / Convert.ToDecimal(upWidth);
                        newHeight = Convert.ToInt32((Convert.ToDecimal(upHeight) * reDuce));
                        newY = (bmpH - newHeight) / 2;

                        newX = 0;
                    }

                    else if (upWidth < upHeight)
                    {
                        reDuce = Convert.ToDecimal(newHeight) / Convert.ToDecimal(upHeight);
                        newWidth = Convert.ToInt32((Convert.ToDecimal(upWidth) * reDuce));
                        newX = (bmpW - newWidth) / 2;
                        newY = 0;
                    }
                    else if (upWidth == upHeight) //
                    {
                        reDuce = Convert.ToDecimal(newHeight) / Convert.ToDecimal(upHeight);
                        newWidth = Convert.ToInt32((Convert.ToDecimal(upWidth) * reDuce));
                        newX = (bmpW - newWidth) / 2;
                        newY = (bmpH - newHeight) / 2;
                    }

                    newGraphic = Graphics.FromImage(newBmp);

                    this.newGraphic.Clear(Color.White);
                    this.newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    this.newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    newGraphic.DrawImage(upBmp, newX, newY, newWidth, newHeight);
                    newBmp.Save(HttpContext.Current.Server.MapPath(filePath), System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                this.upBmp.Dispose();
                this.newBmp.Dispose();
                this.newGraphic.Dispose();
            }
            return imgName;
        }

Upvotes: 0

Views: 426

Answers (1)

zmechanic
zmechanic

Reputation: 1990

You are experiencing JPEG compression artifacts, not geometrical distortion. You need to set JPEG compression quality before saving your image. Here is how you can save your image with highest quality (look for 100L in the code below):

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
    if (codec.FormatID == ImageFormat.Jpeg.Guid)
    {
        var myEncoder = System.Drawing.Imaging.Encoder.Quality;
        var myEncoderParameter = new EncoderParameter(myEncoder, 100L);
        var myEncoderParameters = new EncoderParameters(1) { Param = { [0] = myEncoderParameter } };
        newBmp.Save(@"C:\qqq\111.jpeg", codec, myEncoderParameters);

        break;
    }
}

Here is the MSDN article for it: https://msdn.microsoft.com/en-us/library/bb882583(v=vs.110).aspx

Upvotes: 1

Related Questions