How to resize an image in C #?

2

I'm working on an application that receives images. It turns out that the client sometimes sends these images with a very large resolution, which is not necessary in our case (reaching 7MB).

I need the image to be at most 1200 wide (the resolution is always 3x4).

It turns out that I do not have much experience with C # ...

How to resize an image with C #? Do you need to install any additional libraries, or is there a standard library already?

    
asked by anonymous 08.03.2018 / 13:08

1 answer

5

In the link suggested by @DiegoSantos there is a method that allows you to resize the image.

I tested with a PNG image with a size of 1920x1080 and a size of 2.46 MB. I resized the image to 600x400 and the size was 580 KB, and it still maintained great quality.

Method to resize the image:

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;
}

Explanation of the ResizeImage method

The ResizeImage() method above uses the following classes to change the resolution without affecting image quality:

  • Rectangle is a structure that represents the location and the size of the image
  • Bitmap contains the data for the pixels in the image , also stores image attribute information, as well as being the new image with the resolution changed.
  • Graphics encapsulates the drawing surface GDI . Thanks to this class we maintain the quality.
  • ImageAttributes keeps the information corresponding to the colors and image metadata, that data is manipulated during rendering.

An example implementation of the method:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

...

private void OnClick(object sender, EventArgs e)
{   
    Stream myStream = null;

    openFileDialog.FileName = "MinhaImagem";

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        try
        {
            if ((myStream = openFileDialog.OpenFile()) != null)
            {
                using (myStream)
                {
                    var image = Image.FromStream(myStream);

                    var newImage = ResizeImage(image, 600, 400);
                    newImage.Save("c:\newImage.png", ImageFormat.Png);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }            
}

Explanation of the implementation of the ResizeImage method

In the above implementation I used a Png image and a OpenFileDialog to test, the ResizeImage method returns the modified image, then saved it to disk specifying the image format in the save() method, which in this case is Png.

It is worth remembering that it would be interesting to do some unit tests on this method to see if it returns any unusual results.

Source: link

    
08.03.2018 / 18:04