Home > Backend Development > C++ > How to Resize Images in C# for High Quality?

How to Resize Images in C# for High Quality?

DDD
Release: 2025-01-31 01:16:08
Original
323 people have browsed it

How to Resize Images in C# for High Quality?

C#high -quality image zoom method

It is not easy to adjust the image size when running in C#. Although has ,

and

properties, the setting of these attributes does not adjust the image size. System.Drawing.Image Size To adjust the size of the image, you need to use the new size to create a new Width object and draw the original image to the object. You can use the following code to implement: Height

However, this method will lead to a decline in image quality. To achieve high -quality zoom, the Bitmap class is needed. How to operate the example below:

Bitmap objBitmap = new Bitmap(objImage, new Size(227, 171));
Copy after login

This function provides high -quality zoom in the following ways: Graphics

/// <summary>
/// 将图像调整为指定宽度和高度。
/// </summary>
/// <param name="image">要调整大小的图像。</param>
/// <param name="width">要调整到的宽度。</param>
/// <param name="height">要调整到的高度。</param>
/// <returns>调整大小后的图像。</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;
}
Copy after login
Set to

to prevent heavy shadows around the image boundary.

    Set the target image
  • to keep DPI. WrapMode TileFlipXY Use high -quality
  • ,
  • , Resolution and
  • Settings.
  • Compositing Interpolation Other considerations include the longitudinal ratio of the image (reserved for readers as exercises), and the trap where the image zoom in the image when saving the image. Smoothing

The above is the detailed content of How to Resize Images in C# for High Quality?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template