重新缩放图像

时间:2013-08-07 01:05:07

标签: c# bitmap image-resizing

我正在尝试将图像重新缩放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);

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

1 个答案:

答案 0 :(得分: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类上的this constructor,它允许您将代码简化为:

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