0

图像在裁剪成各种尺寸时会变形。
如何在不影响图像质量的情况下做到这一点?我目前的结果是扭曲的模糊图像。请帮忙。

这是我的代码:

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

1 回答 1

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

于 2016-05-04T10:23:03.997 回答