裁剪图像而不会扭曲c#中的像素

时间:2016-05-04 09:51:45

标签: c# image image-processing bitmap image-quality

将图像裁剪成各种尺寸时,图像会变形。
如何在不影响图像质量的情况下执行此操作?我目前的结果是一个扭曲的模糊图像。请帮忙。

这是我的代码:

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;
        }

1 个答案:

答案 0 :(得分:1)

您遇到JPEG压缩瑕疵,而不是几何失真。您需要在保存图像之前设置JPEG压缩质量。以下是如何以最高质量保存图像(在下面的代码中查找100L):

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;
    }
}

以下是MSDN的文章:https://msdn.microsoft.com/en-us/library/bb882583(v=vs.110).aspx