
How to process Gif image with GDI+ and .NET
February 27, 2007Working with images in .NET is easy process especially when using GDI+ . For this purposes we need to include System.Drawing namespace. and use Image,Bitmap and Graphics classes. This is ok with jpeg, png, bmp … but it is not so easy to manipulate gif images. This is because gifs are indexed pixels and when try to save image to file it is saved with 8 bit per pixels compression. This makes the image which is just saved to looks bad. One way to pass over this issue is as follows:
- Get width and height of the original picture
- Create a bitmap object in code with width and height just like original image and max resolution( 64 bpp )
- Create Graphics object with this non-indexed image
- Set CompositingQuality as high on graphics object
- Load original gif image as unscaled image in graphics object
Bitmap oNonIndexec = new Bitmap(nWidth, nHeight, PixelFormat.Format64bppPArgb);
oNonIndexec.SetResolution(nWidth, nHeight);
Graphics oGraphics = Graphics.FromImage(oNonIndexec);
oGraphics.CompositingQuality = CompositingQuality.HighQuality;
oGraphics.DrawImageUnscaled(oImage, 0, 0);
This keeps quality as high as possible when processing gifs