0

我有图像,但在重新调整大小时质量很差

在此处输入图像描述

我的代码:

string sFile = "D:\\Testing\\" + txtnewname.Text;
Bitmap newImage = new Bitmap(Convert.ToInt32(txtwidth.Text), Convert.ToInt32(txtHeight.Text));
newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

using (Graphics gr = Graphics.FromImage(newImage))
{
    gr.SmoothingMode = SmoothingMode.HighQuality;
    gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
    gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
    gr.DrawImage(image, new Rectangle(0, 0, (Convert.ToInt32(txtwidth.Text)), Convert.ToInt32(txtHeight.Text)));
}
newImage.Save(sFile + ".jpeg", ImageFormat.Jpeg);
4

1 回答 1

0

问题不在于保存的质量,IMO 很好。问题是您保存新图像的纵横比不同。这就是为什么它看起来与原始图像质量不同的原因。

您必须通过强制执行来确保新图像保持原始图像的纵横比。无论您提供什么高度或宽度作为输入,其中之一必须自动设置为保留原始纵横比。

Bitmap originalImage = Bitmap.FromFile(oFile) as Bitmap;
double ar = (double)originalImage.Height/originalImage.Width;
int newHeight = 100;
int newWidth = (int)(newHeight/ar );
Bitmap newImage = new Bitmap(newWidth, newHeight);
//and then do what you want to do
于 2013-09-02T11:49:05.790 回答