Image resizing using C#
Asked Answered
E

4

9

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:

  • Image theory (how various image formats work jpeg, png, tiff etc).?

  • How does the image looses its sharpness when resized? Is there some

  • Are there any free .Net (I am using 4.0) libraries available for doing this? If not can I use any library using com interoperabilty?

Many thanks,

Emia answered 21/6, 2012 at 12:18 Comment(2)
Read up on "color quantization" en.wikipedia.org/wiki/Color_quantizationBrister
The Bitmap(image, width, height) constructor does a pretty credible job without much fuss.Patent
D
40

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);
        }
    }
}
Distrain answered 21/6, 2012 at 12:25 Comment(3)
Thanks Darin. I do not wish to mention the width and height in numbers. Rather they will be mentioned in percentages. To use this I will have to (probably) calculate the height width of the existing images.Emia
Yes, you could do this after loading the srcImage. Between the first and the second using statement. You cuold use srcImage.Width and srcImage.Height to calculate the newWidth and newHeight passed to the Bitmap constructor.Distrain
This doesn't seem to compress the image. Ironically after halving a .tiff file's resolution, it took up double the space.Jacobah
I
11

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);
Icebreaker answered 9/3, 2015 at 22:30 Comment(0)
R
1

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

Rocket answered 21/6, 2012 at 12:25 Comment(0)
O
0

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);
                }
            }
        }
    }
Overblouse answered 29/10, 2023 at 13:5 Comment(1)
Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?Meek

© 2022 - 2025 — McMap. All rights reserved.