C# c语言中不失真像素的裁剪图像#

C# c语言中不失真像素的裁剪图像#,c#,image,image-processing,bitmap,image-quality,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\\TempIma

图像在裁剪成各种尺寸时会失真。
如何在不影响图像质量的情况下进行此操作?我当前的结果是扭曲的模糊图像。请帮忙

这是我的密码:

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;
        }
var common=new common();
string filesPath=HttpContext.Current.Server.MapPath(Const.directoryPath);
字符串imageUrl1=上传ImageToAzure(1123,“\\Configurator\\\ U trunk\\Content\\TempImages\\eddaec5aa33e4b1593b304674a842874.jpeg”,“eddaec5aa33e4b1593b304674a842874_260x190.jpeg”,云存储,容器名称);
字符串cropimage260x190=CropImagewithName(inputStream/*System.Io.Stream*/,260,190,cropidemagename);
公共字符串CropImagewithName(流、整型宽度、整型高度、字符串名称)
{
int bmpW=0;
int-bmpH=0;
string filePath=string.Empty;
string imgName=string.Empty;
尝试
{
{
bmpW=宽度;
bmpH=高度;
int newWidth=bmpW;
int newHeight=bmpH;
imgName=名称;
imgName=imgName.Replace(“-”,”);
filePath=Const.directoryPath+imgName;
this.upBmp=新位图(流);
this.newBmp=新位图(newWidth、newHeight、System.Drawing.Imaging.PixelFormat.Format24bppRgb);
此.newBmp.SetResolution(300300);
int upWidth=this.upBmp.Width;
int SUPERVIGHT=此向上高度;
int newX=0;
int newY=0;
小数减少;
如果(仰角>仰角)
{
reDuce=Convert.ToDecimal(newWidth)/Convert.ToDecimal(upWidth);
新高度=转换为32((转换为特定高度)*减少));
newY=(bmpH-newHeight)/2;
newX=0;
}
否则,如果(上边<仰角)
{
reDuce=Convert.ToDecimal(新高度)/Convert.ToDecimal(新高度);
newWidth=Convert.ToInt32((Convert.ToDecimal(上行)*reDuce));
newX=(bmpW-newWidth)/2;
newY=0;
}
else if(上升高度==上升高度)//
{
reDuce=Convert.ToDecimal(新高度)/Convert.ToDecimal(新高度);
newWidth=Convert.ToInt32((Convert.ToDecimal(上行)*reDuce));
newX=(bmpW-newWidth)/2;
newY=(bmpH-newHeight)/2;
}
newGraphic=Graphics.FromImage(newBmp);
这个。新图形。清晰(颜色。白色);
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);
}
}
捕获(例外)
{
}
最后
{
this.upBmp.Dispose();
this.newBmp.Dispose();
this.newGraphic.Dispose();
}
返回imgName;
}

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


这是MSDN的文章:

看看这个MSDN页面上的例子,它展示了如何保存jpeg,并使用最高100的质量级别,这是最好的。感谢您的回复,我在这里找到了一个解决方案
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;
    }
}