Resizing transparent images efficiently

Resizing a JPEG with c# is easy but when it comes to resizing a transparent GIF or PNG image, things can get a little bit complicated. I’m sure you have faced the situation before when the transparent background turns out to be black after resizing it. So how to resize a gif and maintain it’s transparency? Fortunatly, Microsoft provides us with a library called ImageManipulator that consists in optimizing and quantizing gif images. You can read about this procress here and download the library directly from here

My code below considers you are retrieving the image content from database and will be rendering it on the browser using asp.net, so it receives an array of Byte and returns MemoryStream. I show you how to do that right after.

You can change the settings for Graphics as you wish; however, I set the PixelOffsetMode to none in order to get a better looking for my image at the end, but you can try different settings as you wish.

fabio

 

public static System.IO.MemoryStream GetThumbnail(Byte[] imageContent, int width, int height)
{

    if (imageContent == null || imageContent.Length == 0)
        return null;

    if (width == 0 || height == 0)
        return null;

    System.IO.MemoryStream imageStream = new System.IO.MemoryStream(imageContent);

    System.Drawing.Image imageOriginal = System.Drawing.Image.FromStream(imageStream);

    System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height,
        System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;

    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.None;

    g.DrawImage(imageOriginal, new System.Drawing.Rectangle(0, 0, width, height), 0, 0,
        imageOriginal.Width, imageOriginal.Height,
        System.Drawing.GraphicsUnit.Pixel);

    ImageManipulator.OctreeQuantizer quantizer = new ImageManipulator.OctreeQuantizer(255, 8);

    System.Drawing.Bitmap quantized = quantizer.Quantize(bitmap);

    System.IO.MemoryStream imageThumbnail = new System.IO.MemoryStream(width * height);

    quantized.Save(imageThumbnail, System.Drawing.Imaging.ImageFormat.Gif);

    g.Dispose();

    imageOriginal.Dispose();

    bitmap.Dispose();

    quantized.Dispose();

    return imageThumbnail;

}

 
Then you can render it via asp.net. Just add the code below into the Page_Load and change contentGifFromDatabase:

using (System.IO.MemoryStream resizedImage = GetThumbnail(contentGifFromDatabase, 60, 20))
{

    System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(resizedImage);

    bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);

    Response.ContentType = "image/gif";

    bitmap.Dispose();

    Response.End();

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s