[英]Rescale an image
我正在嘗試將圖像縮小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);
但是當我這樣做時,它將保存在原始圖像上繪制的縮放圖像,並且我不確定如何更正此圖像
您應該將原始位圖復制到新的位圖,並隨需縮放。 示例(未試用):
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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.