图像大小调整显示质量差?

时间:2013-09-02 10:48:07

标签: c# image bitmap

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

enter image description here

我的代码:

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

1 个答案:

答案 0 :(得分: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