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
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#
– Leandro Angelo
@Leandroangelo is web. In case, I would like to resize on the same server.
– Wallace Maxters
Have you tried so? : https://stackoverflow.com/questions/1922040/resize-an-image-c-sharp
– DiegoSantos