简体   繁体   English

重新缩放图像

[英]Rescale an image

I am trying to rescale on image by down by a factor of 10 and save it 我正在尝试将图像缩小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);

but when I do this it saves the scaled image, drawn on the original image and I am unsure of how to correct this 但是当我这样做时,它将保存在原始图像上绘制的缩放图像,并且我不确定如何更正此图像

You should copy the original bitmap to a new bitmap, scaling as you go. 您应该将原始位图复制到新的位图,并随需缩放。 Example (untested): 示例(未试用):

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

Edit: I just noticed this constructor on the Bitmap class, which allows you to simplify your code to this: 编辑:我只是在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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM