作为 Size
,Width
和 Height
是 System.Drawing 的
;Get()
属性。图片
如何在 C# 中在运行时调整 Image 对象的大小?
现在,我正在创建一个新的 Image
使用:
// objImage is the original Image
Bitmap objBitmap = new Bitmap(objImage, new Size(227, 171));
最佳答案
这将执行高质量的调整大小:
/// <summary>
/// Resize the image to the specified width and height.
/// </summary>
/// <param name="image">The image to resize.</param>
/// <param name="width">The width to resize to.</param>
/// <param name="height">The height to resize to.</param>
/// <returns>The resized image.</returns>
public static Bitmap ResizeImage(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;
}
-
wrapMode.SetWrapMode(WrapMode.TileFlipXY)
防止图像边界周围出现重影——简单的调整大小会对超出图像边界的透明像素进行采样,但通过镜像图像我们可以获得更好的样本(此设置非常明显) -
destImage.SetResolution
无论物理尺寸如何都保持 DPI -- 在减小图像尺寸或打印时可能会提高质量 - 合成控制像素与背景的混合方式——可能不需要,因为我们只绘制一件事。
-
graphics.CompositingMode
确定来自源图像的像素是覆盖还是与背景像素组合。SourceCopy
指定在呈现颜色时覆盖背景颜色。 -
graphics.CompositingQuality
确定分层图像的渲染质量级别。
-
-
graphics.InterpolationMode
确定如何计算两个端点之间的中间值 -
graphics.SmoothingMode
指定直线、曲线和填充区域的边缘是否使用平滑(也称为抗锯齿)——可能只适用于矢量 -
graphics.PixelOffsetMode
绘制新图像时影响渲染质量
保持纵横比留给读者作为练习(实际上,我只是不认为这个函数的工作是为你做的)。
此外,this is a good article描述图像大小调整的一些陷阱。上面的函数会覆盖大部分,但是你还是要担心saving .
关于c# - 如何调整图像 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1922040/