调整图像大小C# [英] Resize an Image C#
本文介绍了调整图像大小C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于尺寸
,宽度
,身高
是获取()System.Drawing.Image对象的属性,结果
我该如何调整在运行时的Image对象在C#。
现在,我只是创建一个新的图片
使用:
// objImage是原始图像
位图objBitmap =新位图(objImage,新的大小(227,171));
解决方案
这将执行一个高品质的调整大小:
///<总结>
///将图像大小调整到指定的宽度和高度。
///< /总结>
///< PARAM NAME =形象>至调整图像大小和LT; /参数>
///< PARAM NAME =宽方式>的宽度调整到< /参数>
///< PARAM NAME =高度方式>以调整的高度< /参数>
///<收益方式>调整后的图像< /回报>
公共静态位图ResizeImage(图片形象,诠释的宽度,高度INT)
{
VAR destRect =新的Rectangle(0,0,宽度,高度);
VAR destImage =新位图(宽,高); destImage.SetResolution(image.HorizontalResolution,image.VerticalResolution); 使用(VAR图形= Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; 使用(VAR wrapMode =新ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(图像,destRect,0,0,image.Width,image.Height,GraphicsUnit.Pixel,wrapMode);
}
} 返回destImage;
}
- <$c$c>wrapMode.SetWrapMode(WrapMode.TileFlipXY)$c$c> prevents图像周围的边框鬼影 - 天真的大小调整采样透明像素以外的图像边界,但通过镜像图像,我们可以得到一个更好的样本(此设置是非常明显的)
- <$c$c>destImage.SetResolution$c$c>保持DPI,无论物理尺寸 - 减少图像尺寸或打印时,可能会增加质量
- 像素合成控制如何混合的背景 - 可能并不需要,因为我们只画一件事。
- <$c$c>graphics.CompositingMode$c$c>确定从源图像中的像素是否覆盖或用背景象素被组合。 <一href=\"https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.compositingmode(v=vs.110).aspx\"><$c$c>SourceCopy$c$c>指定当彩色呈现,它覆盖的背景颜色。
- <$c$c>graphics.CompositingQuality$c$c>决定分层图像的渲染质量水平。
- <$c$c>graphics.InterpolationMode$c$c>决定了中间的计算两个端点之间的值
- <$c$c>graphics.SmoothingMode$c$c>指定直线,曲线和填充区域边缘是否使用平滑(也称抗锯齿) - 可能只适用于矢量
- <$c$c>graphics.PixelOffsetMode$c$c>绘制新的图像时,会影响渲染质量
保持纵横比作为练习留给读者(实际上,我不认为这是这个函数的工作是为你做的)。
此外,这是描述一些缺陷与图像大小调整好文章。上述功能将覆盖大多数人,但你仍然担心节能。
As Size
, Width
, Height
are Get() properties of System.Drawing.Image,
How can I resize an Image object at run-time in C#.
Right now, I am just creating a new Image
using:
// objImage is the original Image
Bitmap objBitmap = new Bitmap(objImage, new Size(227, 171));
解决方案
This will perform a high quality resize:
/// <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)
prevents ghosting around the image borders -- naïve resizing will sample transparent pixels beyond the image boundaries, but by mirroring the image we can get a better sample (this setting is very noticeable)destImage.SetResolution
maintains DPI regardless of physical size -- may increase quality when reducing image dimensions or when printing- Compositing controls how pixels are blended with the background -- might not be needed since we're only drawing one thing.
graphics.CompositingMode
determines whether pixels from a source image overwrite or are combined with background pixels.SourceCopy
specifies that when a color is rendered, it overwrites the background color.graphics.CompositingQuality
determines the rendering quality level of layered images.
graphics.InterpolationMode
determines how intermediate values between two endpoints are calculatedgraphics.SmoothingMode
specifies whether lines, curves, and the edges of filled areas use smoothing (also called antialiasing) -- probably only works on vectorsgraphics.PixelOffsetMode
affects rendering quality when drawing the new image
Maintaining aspect ratio is left as an exercise for the reader (actually, I just don't think it's this function's job to do that for you).
Also, this is a good article describing some of the pitfalls with image resizing. The above function will cover most of them, but you still have to worry about saving.
这篇关于调整图像大小C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文