所以我需要一些調整大小。幫助圖像大小調整方法gdi
我發現了兩種不同的方法。
一個看起來是這樣的:
public static Byte[] ResizeImageNew(System.Drawing.Image imageFile, int targetWidth, int targetHeight) {
using(imageFile){
Size newSize = CalculateDimensions(imageFile.Size, targetWidth, targetHeight);
using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppRgb)) {
newImage.SetResolution(imageFile.HorizontalResolution, imageFile.VerticalResolution);
using (Graphics canvas = Graphics.FromImage(newImage)) {
canvas.SmoothingMode = SmoothingMode.AntiAlias;
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.DrawImage(imageFile, new Rectangle(new Point(0, 0), newSize));
MemoryStream m = new MemoryStream();
newImage.Save(m, ImageFormat.Jpeg);
return m.GetBuffer();
}
}
}
}
另:
public static System.Drawing.Image ResizeImage(System.Drawing.Image originalImage, int width, int maxHeight) {
originalImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
originalImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
int NewHeight = originalImage.Height * width/originalImage.Width;
if (NewHeight > maxHeight) {
// Resize with height instead
width = originalImage.Width * maxHeight/originalImage.Height;
NewHeight = maxHeight;
}
System.Drawing.Image newImage = originalImage.GetThumbnailImage(width, NewHeight, null, IntPtr.Zero);
return newImage;
}
我基本上是 '借' 兩種方法,而只是改變的點點滴滴。
但是 - 使用第一個,每當我調整到一個小一點的圖片,文件的大小實際上是更大的,則原
而且大大提高了第二,而尺寸看起來很可怕:/(!?)
我當然傾向於僅僅用第一種方法改善圖像質量,如果可能的話,但我看不出來,從我的角度看,一切看起來都是「高質量」的?