Mital Kakaiya [MCSD.NET]
In Programming, Logic is everything.

Resize Image - A better way

Thursday, June 09, 2005 6:44 PM
I have created "Resources" project to store all resource information across projects like icons, images, string, messages, etc... This helps my team to develop application faster, more robust, easy to update resources, and most important, globalization supports.

To reduce overhead of the resources with an application interaction, I have created a simple ArrayList collection with a singleton class. It loads all images and icons inside memory when application starts. (I know it occupies lots of memory, so I have to be really careful here. Also, Thanks to my colleague for his support).

When I set image/icon to any button/toolbar etc.. (mainly Infragistics controls), it streaches icon/image to appropriate size to fit (16x16 pixels), which creates an horrible image !!

so I have "google" few articles to find-out best way to resize an image with better quality. Using Window's GDI+ through .NET framework, I have created simple function ImageResize as follow:

This function also helps to create a thumbnail of the large images for web applications, which reduce network traffic and improve response-time.

At last, Good GDI+ FAQ with dotnet examples: here.


Feedback

# re: Resize Image - A better way

Hi Mital,

Thanks for the code snippet, I have a project on the back burner where I will need to generate thumbnails so this code should help to put me on the right track. Thanks again! 6/11/2005 3:30 AM | Peter Stathakos

# But, why are the image files so large ?!

Hi Mital,
great bit of code, works like a charm, but... why are the generated thumbnails sometimes several times larger than the original file !
One of the reasons for generating thumbnails was to save disc space, but none of the ASP.NET (VB) solutions I've found helped.
I've actually resorted to using a Persits component to do the resize once I've uploaded the original image !
Is it an ASP.NET / GDI+ issue, or am I missing something *really* obvious !
email: Name (from above) at gmail dot com 7/11/2005 2:36 PM | OffBeatMammal

# re: Resize Image - A better way

Great!!! Works perfectly!! 8/12/2005 11:23 AM | Nirondes

# re: Resize Image - A better way

Nice code.. I have some weeks trying to do that.. thank you! 10/11/2005 3:27 AM | Roly

# re: Resize Image - A better way

Excelente código, habia revisado otros codigos pero este es el mejor que he encotnrado, Gracias!! 8/5/2006 1:10 AM | armadillo

# re: Resize Image - A better way

Thanks very much, i got the solution with this peace of code.. 5/18/2008 5:13 PM | pavan

# re: Resize Image - A better way

Thanks for this. Here it is translated into C#:

[code]
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace Viz
{
public static class Utilities
{
static readonly PixelFormat[] BadFormats = new PixelFormat[]
{
PixelFormat.Format1bppIndexed ,
PixelFormat.Format4bppIndexed ,
PixelFormat.Format8bppIndexed ,
PixelFormat.Undefined ,
PixelFormat.DontCare ,
PixelFormat.Format16bppArgb1555 ,
PixelFormat.Format16bppGrayScale
};

public static Bitmap Resize(Image src, int height, int width)
{
var b = new Bitmap(width, height, src.PixelFormat);

if (Array.Exists(BadFormats, pf => pf == b.PixelFormat))
throw new NotSupportedException("Pixel format of the image is not supported: "
+ "See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/"
+ "cpref/html/frlrfSystemDrawingGraphicsClassFromImageTopic.asp");

var graphicsImage = System.Drawing.Graphics.FromImage(b);
graphicsImage.SmoothingMode = SmoothingMode.HighQuality;
graphicsImage.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsImage.DrawImage(src, 0, 0, b.Width, b.Height);
graphicsImage.Dispose();
return b;
}
}
}
[/code] 4/10/2009 9:59 AM | Gabe

Comments have been closed on this topic.