I am trying to rescale on image by down by a factor of 10 and save it
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 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");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.