Dealing with Indexed Pixel Formats: Lessons in the ConTrak Badge Printery
 
 

What is the difference between a canvas image and one in a magazine?  Plenty, even if they are depicting the same work.  For starters, the texture and composition between a canvas and piece of paper holds different properties when paint or ink is applied to it.  One's selection of tools also changes with respect to the medium.

This is the same in the .Net Framework when manipulating images.  In .Net, one has three "mediums" from which to choose: Bitmap, Graphics, and Image.  For the most part, Bitmap and Image are closely related--moreso than they are with Graphics.  (For the remainder of this article, "image" refers to a JPEG or related graphical representation while "Image" refers to the System.Drawing.Image class.)

As some are aware, my "ConTrak Systems" convention management software contains an automatic badge printer.  The user will upload the convention's badge image for storage on the server and ConTrak will use that image to create convention badges on demand.  To do this, I must "write" on an in-memory copy of the badge image by using the .Net System.Drawing.Graphics class (I use DrawString and FillRectangle methods under an instantiated Graphics object to accomplish this). 

Early in development, I ran into a problem; one may not instantiate a Graphics class with an image which an indexed pixel format.  I did not pay it much mind.  After all, what are the odds a user will upload an image with a pixel index?  Pretty good--make that inevitable.  In reality, it was the first image a user uploaded.  Instead of force users to save files differently, I don't even think I could tell a user how to get rid of a JPEG's pixel index, I decided to take it upon myself to discover a solution.


 


 

Problem: Enable images with a pixel index to be loaded within a Graphics class from an Image class (i.e. using System.Drawing.Graphics.FromImage(image) to create an instance of Graphics).  System.Drawing.Imaging.PixelFormat cannot be changed within an instantiated Image, Bitmap, or Graphics object.


 

Solution: Either upon upload or before creating a convention badge I must detect pixel indicies and remove them.  The solution is quite simple implemented, but it required a great deal of consideration and discovery before it was discovered (much to the tune of many solutions). 

The solution involves detecting the index (there are four types of indexed pixel formats total), making the image a bitmap, cloning the bitmap to another (new) bitmap object with the pixel format changed, and saving that bitmap.  The solution to this problem is in the below method.  Make certain the file is closed before performing this method, as this method requires the image file to be unlocked from processes so it may be overwritten.


 


public void CheckPixelIndex(string imageLoc)
{
    //Open the image to test for a pixel index.
    System.Drawing.Image img = System.Drawing.Image.FromFile(imageLoc);

    //Shortening what I have to type:
    System.Drawing.Imaging.PixelFormat imgFormat = img.PixelFormat;

    //These are the indices for which I need to check:
    if (imgFormat == System.Drawing.Imaging.PixelFormat.Format1bppIndexed ||
        imgFormat == System.Drawing.Imaging.PixelFormat.Format4bppIndexed ||
        imgFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed ||
        imgFormat == System.Drawing.Imaging.PixelFormat.Indexed)
    {

        //I need to convert that image format to something else, and I don't care
            what else because it MUST be converted.


        System.Drawing.Bitmap bmap = new System.Drawing.Bitmap(img);
        System.Drawing.Bitmap newmap = bmap.Clone(new System.Drawing.RectangleF(0,0,bmap.Width,
            bmap.Height), System.Drawing.Imaging.PixelFormat.DontCare);

        //Release the file before overwriting it:
        bmap.Dispose();
        bmap = null;
        img.Dispose();
        img = null;

        //Save the file and release access to it:
        newmap.Save(imageLoc);
        newmap.Dispose();
    }
    else
    {

        //If it got here, it's save to use in Graphics; release file holds:
        img.Dispose();
        img = null;
    }

}

 



Dev Log Entries

Air Hockey (4)

Commentary (3)

ConTrak (16)

CSC Picture Project (3)

Dev Log (12)

DevFolio (5)

FinGame (1)

Geocaching (1)

Haunted Pictures (7)

Misc (1)

Personal Life (13)

PhoenixPo.com (4)

Photography (5)

Web Programming (3)

WOES (1)

 

Total Entries: 79

 

Haunted Attraction Pictures     Statbar Modifier     CSC Picture Project     DevFolio.com