How to resize an image in c#?

Asked

Viewed 1,940 times

2

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

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

Turns out I don’t have much experience with C#...

How to resize an image with C#? It is necessary to install some additional library, or is there already a standard library?

  • 3

    is it a desktop or web application? if it is web worth resizing it in the client before sending, but if bandwidth is no problem in the transfer it is very easy to solve it in c#

  • @Leandroangelo is web. In case, I would like to resize on the same server.

  • Have you tried so? : https://stackoverflow.com/questions/1922040/resize-an-image-c-sharp

1 answer

6

In the link suggested by @Diegosantos there is a method that allows resizing the image.

I did a test with a PNG image with the size 1920x1080 and the size 2.46 MB. I resized the image to 600x400 and the size was 580 KB, and maintained a great quality yet.

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 method ResizeImage() above uses the following classes to change resolution without affecting image quality:

  • Rectangle is a structure representing the location and size of the image
  • Bitmap contains the image pixel data, also stores image attribute information, and is 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 metadata of the image, this data is manipulated during rendering.

An example of method implementation:

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 for testing, the Resizeimage method returns the modified image, then saved it to disk by specifying the image format in the method save() which in this case is Png.

Remember that it would be interesting to do some unit tests in this method to see if it returns any unusual results.

Source: https://stackoverflow.com/a/24199315/5429980

Browser other questions tagged

You are not signed in. Login or sign up in order to post.