本文介绍了如何调整图像C#的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 由于大小,宽度和高度是 System.Drawing.Image 的code> Get()属性; 如何调整Image对象的大小As Size, Width and Height are Get() properties of System.Drawing.Image;How can I resize an Image object at run-time in C#?现在,我只是使用以下命令创建一个新的 Image :Right now, I am just creating a new Image using:// objImage is the original ImageBitmap 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) 防止图像边界周围出现鬼影-幼稚的调整大小将对超出图像边界的透明像素进行采样,但是通过镜像图像,我们可以获得更好的采样(此设置非常引人注目) destImage。 SetResolution 保持DPI大小,无论其物理尺寸如何-减小图像尺寸或打印时可能会提高质量 合成控制像素如何与背景混合-可能不需要,因为我们只画一件事。 graphics.CompositingMode 确定源图像中的像素是覆盖还是与背景像素组合。 SourceCopy 指定在渲染颜色时,它将覆盖背景颜色。 graphics.CompositingQuality 确定分层图像的渲染质量级别。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 printingCompositing 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.锻炼读者(实际上,我只是认为为您做到这一点不是此功能的工作)。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#的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!