所以我需要一些调整大小。帮助图像大小调整方法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;
}
我基本上是 '借' 两种方法,而只是改变的点点滴滴。
但是 - 使用第一个,每当我调整到一个小一点的图片,文件的大小实际上是更大的,则原
而且大大提高了第二,而尺寸看起来很可怕:/(!?)
我当然倾向于仅仅用第一种方法改善图像质量,如果可能的话,但我看不出来,从我的角度看,一切看起来都是“高质量”的?