Oxyprogrammer
Oxyprogrammer

Reputation: 1263

Image resizing using C#

The images that are fresh out from digital cameras are often above the size of 2-3 MB making it difficult for it to get transferred over email and other ways. This requires the image to be resized (in terms of file size and not height or width). Quite similar to MS Paint offering image resizing functionality. I am not well educated on image file theories. I would appreciate if someone can point me towards following information sources:

Many thanks,

Upvotes: 9

Views: 44023

Answers (4)

Maarten Kronberger
Maarten Kronberger

Reputation: 1

I have made a small change to the code above so that it can handle transparency also.

 public void Resize(string imageFile, string outputFile, int intNewWidth, int intNewHeight)
    {
        using (var srcImage = System.Drawing.Image.FromFile(imageFile))
        {
            var newWidth = (int)intNewWidth;
            var newHeight = (int)intNewHeight;
            using (var newImage = new Bitmap(newWidth, newHeight)) 
            {
                using (var graphics = Graphics.FromImage(newImage))
                {
                    graphics.SmoothingMode = SmoothingMode.AntiAlias;
                    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    graphics.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
                    newImage.MakeTransparent();
                    newImage.Save(outputFile);
                }
            }
        }
    }

Upvotes: 0

Dima Sherba
Dima Sherba

Reputation: 152

I used the example provided by Darin Dimitrov, Image was inflated to and took up a lot of disk space (from 1.5MB to 17MB or so).

This is due to a small mistake in the last line of code.

The function below will save the image as Bitmap (huge image size).

newImage.Save(outputFile)

The correct function should be:

newImage.Save(outputFile, ImageFormat.Jpeg);

Upvotes: 11

Darin Dimitrov
Darin Dimitrov

Reputation: 1039398

Image resizing is functionality is built right into the .NET framework. There are a couple of different approaches:

  • GDI+
  • WIC
  • WPF

Here's a nice blog post covering the differences between them.

Here's an example with GDI+:

public void Resize(string imageFile, string outputFile, double scaleFactor)
{
    using (var srcImage = Image.FromFile(imageFile))
    {
        var newWidth = (int)(srcImage.Width * scaleFactor);
        var newHeight = (int)(srcImage.Height * scaleFactor);
        using (var newImage = new Bitmap(newWidth, newHeight))
        using (var graphics = Graphics.FromImage(newImage))
        {
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            graphics.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
            newImage.Save(outputFile);
        }
    }
}

Upvotes: 40

Remy
Remy

Reputation: 12713

Imageresizer works well. http://imageresizing.net/

Upvotes: 1

Related Questions