调整图像大小C# [英] Resize an Image C#

查看:1096
本文介绍了调整图像大小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.Horizo​​ntalResolution,image.VerticalResolution);    使用(VAR图形= Graphics.FromImage(destImage))
    {
        graphics.CompositingMode = CompositingMode.SourceCopy;
        graphics.CompositingQuality = CompositingQuality.HighQuality;
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphics.Smoothi​​ngMode = Smoothi​​ngMode.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;
}

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

此外,这是描述一些缺陷与图像大小调整好文章。上述功能将覆盖大多数人,但你仍然担心节能

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.InterpolationMode determines how intermediate values between two endpoints are calculated
  • graphics.SmoothingMode specifies whether lines, curves, and the edges of filled areas use smoothing (also called antialiasing) -- probably only works on vectors
  • graphics.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆