Lossless Image Converting in C# - Taking a Break
(Page 4 of 4 )
Finally we have created the groundwork for our Lightweight Image Manipulation application. At this stage our little utility software is able to convert images. Certainly we cannot say that's some outstanding function, but still, the purpose of this article was to learn how to design, implement, and code these kinds of applications.
The attached source code for the first part, because the resizing is going to be covered in the next segment, can be found below. It is a ZIP archive and you should just extract the whole archive to your chosen destination. As I already mentioned, the project was developed with Visual Studio 2005.

I also want to point out the fact that if you play around with the image conversion application we've just finished programming, you may notice that if you convert to GIF extension, then the conversion by no means will be lossless. The destination image becomes very pixelated and a bit blurry. Some of the color palettes become misplaced. I am going to attach two example images so you can catch my drift easier.

(The image on left is in original JPG format, the one on right is converted to GIF.)
"Why does that happen?" In short, it happens because of color quantization. Regardless of the encoder settings (such as high quality) we choose, our image quality gets destroyed due to the loss conversion. A better term for color quantization is color reduction. However, the de facto standard usage in DSP and computing is quantization.
By its very nature, the GIF extension format doesn't allow more than 256 palette entries. And an image always has a particular number of colors in each of its palettes. Therefore, quantization is required for each GIF image file, especially when converting 16-, 24- or higher bit images. It is necessary to do a quantization algorithm when resizing any image to GIF format. With other formats, this isn't an issue.
So as you had noticed, the "default" quantization algorithm of .NET's GDI+ is optimized for speed and not quality. It is a web development environment and it makes sense that speed is more important than quality, especially back in those days when we were struggling with dial-up connections. To improve our GIF conversion, we'll stick with an independent and different quantization algorithm, but that's for the next part!
Until next time, keep coding and remember - Perfect practice makes perfect!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |