[英]Trying to resize and compress height only maintaining aspect ratio with good quality
[英]How to resize images with maintaining quality
我正在嘗試將圖像尺寸調整為500x500像素。 使用以下代碼:
public static Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
但是,這只是水平調整大小,沒有進行裁剪! 有人建議我將圖像裁剪為中心的1:1比例,然后將其縮小。 如果圖像尺寸小於500x500,我也希望將圖像尺寸增加到500x500。
我該如何實現?
代替
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
采用
Rectangle cropRect = new Rectangle(0,0,500,500);
graphics.DrawImage(image, destRect, cropRect, GraphicsUnit.Pixel);
並且您還可以閱讀更多有關DrawImageUnscaledAndClipped
的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.