0

我正在尝试将图像重新缩放 10 倍并保存它

System.Drawing.Bitmap bmi     
var scaleWidth = (int)(bmi.Width * 0.1);
var scaleHeight = (int)(bmi.Height * 0.1);
Rectangle f = new Rectangle(0, 0, scaleWidth, scaleHeight);
Graphics graph = Graphics.FromImage(bmi);           
graph.DrawImage(bmi, new Rectangle(
    ((int)scaleWidth - scaleWidth) / 2, 
    ((int)scaleHeight - scaleHeight) / 2, 
    scaleWidth, scaleHeight));
string a = "a.jpg";
bmi.Save(a);

但是当我这样做时,它会保存在原始图像上绘制的缩放图像,我不确定如何纠正这个问题

4

1 回答 1

0

您应该将原始位图复制到新位图,并随时缩放。示例(未经测试):

Bitmap bmpOriginal = /* load your file here */
var scaleWidth = (int)(bmi.Width * 0.1);
var scaleHeight = (int)(bmi.Height * 0.1);
Bitmap bmpScaled = new Bitmap(scaleWidth, scaleHeight);
Graphics graph = Graphics.FromImage(bmpScaled );           
graph.DrawImage(bmpOriginal, new Rectangle(0, 0, scaleWidth, scaleHeight);
bmpScaled.Save("scaledImage.bmp");

编辑:我刚刚注意到Bitmap 类上的这个构造函数,它允许您将代码简化为:

Bitmap bmpOriginal = /* load your file here */
var scaleWidth = (int)(bmi.Width * 0.1);
var scaleHeight = (int)(bmi.Height * 0.1);
Bitmap bmpScaled = new Bitmap(bmpOriginal, new Size(scaleWidth, scaleHeight));
bmpScaled.Save("scaledImage.bmp");
于 2013-08-07T01:11:26.003 回答