c# - 如何调整图像 C#

标签 c# image resize

作为 SizeWidthHeightSystem.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;
}

保持纵横比留给读者作为练习(实际上,我只是不认为这个函数的工作是为你做的)。

此外,this is a good article描述图像大小调整的一些陷阱。上面的函数会覆盖大部分,但是你还是要担心saving .

关于c# - 如何调整图像 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1922040/

相关文章:

jquery - 使用函数

c# - self 跟踪实体 ListView 绑定(bind)在更改时失去值(value)

java - 将图像从android上传到java servlet并保存

image - 失败时重新加载缓存的网络图像

c++ - 来自 Mat 图像的 OpenCV 子图像

css - Firefox 溢出 :scroll resize issue

javascript - 下拉复选框选择取消选择故障

c# - 为什么我不能在 foreach 循环中设置迭代变量的属性?

c# - 使用另一个接口(interface)从一个接口(interface)实现泛型方法

objective-c - 在为用户调整大小和关闭时保存 NSWindow 大小