C#
  Home arrow C# arrow Page 2 - Lossless Image Resizing in C#
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Lossless Image Resizing in C#
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2008-02-20

    Table of Contents:
  • Lossless Image Resizing in C#
  • Color Quantization
  • Getting Started
  • Let's Resize!
  • Completing the Project
  • Final Words

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Lossless Image Resizing in C# - Color Quantization


    (Page 2 of 6 )

    Let's remind ourselves of the way we defined quantization and why it is necessary for GIF conversions. First, we all know that JPEG images are full-colored and that the format is able to store 24 bit images with the appropriate palettes. This means that when you want to display a 24-bit or higher bit JPEG image on 8-bit hardware, for example, the image viewer application quantizes the colors to 8 bit.

    Thus, the display process becomes drastically slow - of course, this depends on the hardware of the system as well as the algorithm that's used by the image viewer. In a nutshell, color quantization stands for color reduction, so it matches the hardware capabilities and therefore the system is able to display the images.

    Needless to say, we are living in a generation where having 24-bit colors is "the norm," so this isn't an issue with JPEG and other formats. Fortunately, by their very nature, as I explained before, they can support up to 24-bit and more colors. That's why our JPEG, PNG, BMP, etc. images are so razor sharp and high quality.

    But the scenario is different in the case of GIFs. The GIF format was designed with weak system performance in mind, especially 8-bit or less color display possibilities. Due to this, by definition the GIF image format doesn't allow more than 256 color palette entries; each palette contains a specific number of colors. This means that an image saved/converted to the GIF format cannot contain more than 8-bit colors.

    You see, the aforementioned fact explains why color quantization is a must when we convert higher than 8-bit colored images to the GIF format. The GDI+ of the .NET environment is optimized for speed and because of this, it sort of neglects image quality. Thus, the default quantization algorithm incorporated by Microsoft, unfortunately, produces pixelated and low quality images.

    The quantization algorithm is very dependent upon the source image. Sometimes is looks quite acceptable, other times the image becomes almost totally destroyed due to hundreds of misplaced and wrongly colored pixels, resulting in all-around pixelation.

    In the first part of this series, I attached a comparison picture where an original 24-bit JPEG image is compared to an 8-bit quantized image done with the GDI+ standard algorithm. It turned out quite awful. Here we show another example. The results are "acceptable," but the loss is clearly visible.

    (The left photo is 24-bit original JPEG while the right one is the quantized 8-bit GIF.)

    In a nutshell, quantization is necessary each time we work with GIFs because by the format's very nature it cannot accept more than 256 colors. Therefore, we need to design a quantization algorithm which reduces the original source image's bit-depth to 8-bit. Developing a high-quality color quantization algorithm from scratch is a black art and a very complicated process. Add to this the fact that there does not exist an algorithm that is optimal for every possible image. It is impossible.

    As we've presented here, it is clear enough that in this era of computing GIFs are quite obsolete. That's why nowadays you don't really come across the GIF format, excluding the cases when the GIF in question is animated. Animations of 8-bit or less are acceptable, since their size grows linearly to the number of frames anyway.

    Notwithstanding, we don't want to leave this part unresolved in our application, so we brainstorm for a solution. Searching Google for "good color quantization algorithm" is a way to find answers. I have come across Wesley Bakker's Better Image Processor [BIP] project that is freely distributed in its DLL form at his site. You can download the whole BIP in its entirety containing 3 DLLs, but we are only going to need one of them.

    More C# Articles
    More By Barzan "Tony" Antal


       · Thanks for following this series. This is the second and last part of the "Lossless...
     

    C# ARTICLES

    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek