<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-24915814</id><updated>2011-07-28T06:02:39.357-06:00</updated><title type='text'>Caution, bmp in Road</title><subtitle type='html'>A blog for presenting GDI+ concepts and techniques using Microsoft VB.NET and C#.  Topics covered will range all over the System.Drawing namespace.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://bmpinroad.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/24915814/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://bmpinroad.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>colithium</name><uri>http://www.blogger.com/profile/03625900788586728203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-24915814.post-114420211348079889</id><published>2006-04-04T19:28:00.000-06:00</published><updated>2006-04-04T19:56:22.016-06:00</updated><title type='text'>File Formats, Pixel Formats</title><content type='html'>&lt;p class="MsoNormal" style="margin-bottom: 12pt;"&gt;    I thought it would be a good idea to kick off this blog with a lesson on the different formats an image can take. There are two distinct things to keep in mind here. The first is the &lt;i&gt;file format&lt;/i&gt; that the image is saved in. GDI+ comes with several handy image encoders and decoders.&lt;/p&gt;  &lt;table class="MsoNormalTable" style="" border="1" cellpadding="0" cellspacing="3"&gt;  &lt;tbody&gt;&lt;tr style=""&gt;   &lt;td style="padding: 0.75pt;" valign="top"&gt;   &lt;p class="MsoNormal"&gt;&lt;br /&gt;&lt;b&gt;Built-in GDI+ Image Encoders&lt;/b&gt;:&lt;br /&gt;&lt;br /&gt;Codec Name: Built-in BMP Codec&lt;br /&gt;File Extension(s): *.BMP;*.DIB;*.RLE&lt;br /&gt;Mime Type: image/bmp&lt;br /&gt;&lt;br /&gt;Codec Name: Built-in JPEG Codec&lt;br /&gt;File Extension(s): *.JPG;*.JPEG;*.JPE;*.JFIF&lt;br /&gt;Mime Type: image/jpeg&lt;br /&gt;&lt;br /&gt;Codec Name: Built-in GIF Codec&lt;br /&gt;File Extension(s): *.GIF&lt;br /&gt;Mime Type: image/gif&lt;br /&gt;&lt;br /&gt;Codec Name: Built-in TIFF Codec&lt;br /&gt;File Extension(s): *.TIF;*.TIFF&lt;br /&gt;Mime Type: image/tiff&lt;br /&gt;&lt;br /&gt;Codec Name: Built-in PNG Codec&lt;br /&gt;File Extension(s): *.PNG&lt;br /&gt;Mime Type: image/png&lt;/p&gt;   &lt;/td&gt;   &lt;td style="padding: 0.75pt;" valign="top"&gt;   &lt;p class="MsoNormal"&gt;&lt;br /&gt;&lt;b&gt;Built-in GDI+ Image Decoders&lt;/b&gt;:&lt;br /&gt;&lt;br /&gt;Codec Name: Built-in EMF Codec&lt;br /&gt;File Extension(s): *.EMF&lt;br /&gt;Mime Type: image/x-emf&lt;br /&gt;&lt;br /&gt;Codec Name: Built-in WMF Codec&lt;br /&gt;File Extension(s): *.WMF&lt;br /&gt;Mime Type: image/x-wmf&lt;br /&gt;&lt;br /&gt;Codec Name: Built-in ICO Codec&lt;br /&gt;File Extension(s): *.ICO&lt;br /&gt;Mime Type: image/x-icon&lt;br /&gt;&lt;br /&gt;The framework has Decoders for every Encoder in addition to the ones listed   above.&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt;  &lt;p class="MsoNormal"&gt;&lt;br /&gt;&lt;br /&gt;As you can see, we can open BMP, JPG, GIF, TIF, PNG, EMF, WMF, and ICO file formats. What you need to realize is that all formats are converted to a Bitmap like format once opened. This means that while residing in memory, there are no compression algorithms in play, and everything is a raster image (EMF and WMF are converted from their metafile formats to the Bitmap's raster format when they are opened based on the Width and Height of the Bitmap object). A Bitmap is used as the memory representation of the image because it can display every color that your monitor can (we'll get into Color Depth and Pixel Format later), it supports transparency (the Alpha channel), and it can hold multiple frames (although you cannot work with them due to lack of support from GDI+, you are allowed to at least view what is already there). By saying that they are a Bitmap while in memory is not to say that they magically gain image quality (compression techniques of GIF, JPG, and TIF can reduce quality/amount of colors). The image keeps its exact same appearance, only now it takes up the amount of space required by a Bitmap (which equates to more space if you’re coming from a compressed format).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This conversion is a very good thing for us individuals who need to edit or otherwise manipulate pixels within an image. Image if you had to keep in mind the details of how a particular pixel you want to change was compressed and if your change will affect other pixels around it, if it will render the compression useless, if the new color is in the color palette, etc. Display is also much more efficient because graphics cards display images as is (Bitmap style).&lt;br /&gt;&lt;br /&gt;Once an image is ready to be saved, file format comes back into play. Seeing as how EMF and WMF are metafile formats, we can't really save to them since all we have is a raster image (note that there are no EMF or WMF &lt;i&gt;encoders&lt;/i&gt;.  Metafiles are handled by the &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdrawingimagingmetafileclasstopic.asp"&gt;Metafile class&lt;/a&gt; and are outside the scope of this blog besides what is mentioned about opening them as a raster image). The raw, uncompressed pixel data is sent to the file type's respective encoder; the encoder does its magic and saves the image in that particular format. Some encoders accept optional parameters to control the encoding process. The JPG encoder accepts a &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDrawingImagingEncoderClassQualityTopic.asp"&gt;Quality&lt;/a&gt; parameter (0-100, represents a percent of quality) and the TIF encoder accepts a parameter that determines which TIF &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdrawingimagingencoderclasscompressiontopic.asp"&gt;Compression&lt;/a&gt; algorithm is used (None, CCITT3, CCITT4, LZW, or RLE). &lt;span style=""&gt; &lt;/span&gt;There is also the &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdrawingimagingencoderclasscompressiontopic.asp"&gt;ColorDepth&lt;/a&gt; parameter which specifies bits per pixel (PixelFormat).  This applies to encoders that have a non-compressing mode (ColorDepth doesn't make sense when talking about an image with a &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdrawingimagingcolorpaletteclasstopic.asp"&gt;ColorPalette&lt;/a&gt; or that is compressed).&lt;span style=""&gt;  &lt;/span&gt;GDI+ supports these parameters by providing a nice overload of Bitmap.Save(FileName, &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdrawingimagingimagecodecinfoclasstopic.asp"&gt;ImageCodecInfo&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdrawingimagingencoderparametersclasstopic.asp"&gt;EncoderParameters&lt;/a&gt;).&lt;br /&gt;&lt;br /&gt;Now that we have covered the transfer to and from a storage medium, let's talk about the image’s format while it's actually open in memory.  This brings us to &lt;i&gt;pixel format&lt;/i&gt;.  An image is made up of rows and columns of pixels.  One pixel can consist of three or more color channels.  There is a Red, Green, and Blue channel in GDI+'s model.  An Alpha channel (how "see-through" the pixel is) may also be present in some formats.  An image's pixel format simply describes how many bits make up each channel.&lt;span style=""&gt;  &lt;/span&gt;The different formats that are supported by GDI+ can be found in the &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdrawingimagingpixelformatclasstopic.asp"&gt;PixelFormat&lt;/a&gt; enumeration.&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/p&gt;  &lt;table class="MsoTableGrid" style="border: medium none ; border-collapse: collapse;" border="1" cellpadding="0" cellspacing="0"&gt;  &lt;tbody&gt;&lt;tr style=""&gt;   &lt;td style="border: 1pt solid windowtext; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Alpha&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Canonical&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Gdi&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;DontCare&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Undefined&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Extended&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;PAlpha&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Indexed&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Format1bppIndexed&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Format4bppIndexed&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Format8bppIndexed&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Format16bppGrayScale&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Format16bppRgb555&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Format16bppRgb565&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Format16bppArgb1555&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Format24bppRgb&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Format32bppRgb&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Format32bppArgb&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Format32bppPArgb&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Format48bppRgb&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Format64bppArgb&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 125.45pt;" valign="top" width="167"&gt;   &lt;p class="MsoNormal"&gt;Format64bppPArgb&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt;  &lt;p class="MsoNormal"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style=""&gt;            &lt;/span&gt;Let’s try to make sense of this badly formed enumeration shall we?&lt;span style=""&gt;  &lt;/span&gt;Only pay attention to formats beginning with the prefix “Format”, all the others mean basically nothing.&lt;span style=""&gt;  &lt;/span&gt;The number preceding ‘bpp’ refers to how many bits are taken up by one pixel.&lt;span style=""&gt;   &lt;/span&gt;‘A’ stands for “Alpha”, ‘PA’ stands for “Pre-multiplied Alpha”, ‘R’/‘r’ stands for “Red”, ‘g’ stands for “Green”, and ‘b’ stands for “Blue”, these stand for the color channels of the pixel.&lt;span style=""&gt;  &lt;/span&gt;If there is a string of numbers after the channel combinations, they should be read so that they correspond with the color channel (Argb1555 means ‘1’ bit for ‘A’, ‘5’ bits for ‘r’, ‘5’ bits for ‘g’, and ‘5’ bits for ‘b’).&lt;span style=""&gt;  &lt;/span&gt;If these numbers don’t add up to the bpp, it means the remaining bit(s) aren’t used.&lt;span style=""&gt;  &lt;/span&gt;The ‘Indexed’ suffix means that a ColorPalette is used.&lt;span style=""&gt;  &lt;/span&gt;The ‘GrayScale’ suffix that there is only one color channel and it should be interpreted as a shade of gray.&lt;span style=""&gt;  &lt;/span&gt;It’s also funny to note that anything above 32bpp doesn’t work correctly.&lt;span style=""&gt;  &lt;/span&gt;That’s right, they don’t work (which is fine seeing that today’s monitors can only display 8 bits per color channel anyway).&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style=""&gt;            &lt;/span&gt;Another important point, you can’t draw on (create a Graphics object from) any Bitmap that has an Indexed, or GrayScale suffix, or 16bppArgb1555.&lt;span style=""&gt;  &lt;/span&gt;Although you can’t draw on them, you can still open them up and stick them in a Bitmap object (remember what I said earlier about the difficulties of changing a compressed Bitmap or one with a ColorPalette, GrayScale is a different story, I can’t think of a reason why it’s not supported).&lt;span style=""&gt;  &lt;/span&gt;Knowing this, we can now break down the enumeration into more intelligent pieces.&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;Formats you can create a Bitmap with AND create a Graphics from the resulting Bitmap object AND correctly draw on with:&lt;/p&gt;  &lt;table class="MsoTableGrid" style="border: medium none ; border-collapse: collapse;" border="1" cellpadding="0" cellspacing="0"&gt;  &lt;tbody&gt;&lt;tr style=""&gt;   &lt;td style="border: 1pt solid windowtext; padding: 0in 5.4pt; width: 131.4pt;" valign="top" width="175"&gt;   &lt;p class="MsoNormal"&gt;Format16bppRgb555&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 131.4pt;" valign="top" width="175"&gt;   &lt;p class="MsoNormal"&gt;Format16bppRgb565&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 131.4pt;" valign="top" width="175"&gt;   &lt;p class="MsoNormal"&gt;Format24bppRgb&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 131.4pt;" valign="top" width="175"&gt;   &lt;p class="MsoNormal"&gt;Format32bppRgb&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 131.4pt;" valign="top" width="175"&gt;   &lt;p class="MsoNormal"&gt;Format32bppArgb&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 131.4pt;" valign="top" width="175"&gt;   &lt;p class="MsoNormal"&gt;Format32bppPArgb&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt;  &lt;p class="MsoNormal"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;Formats you can create a Bitmap with AND create a Graphics from the resulting Bitmap object BUT can’t correctly draw on with:&lt;/p&gt;  &lt;table class="MsoTableGrid" style="border: medium none ; border-collapse: collapse;" border="1" cellpadding="0" cellspacing="0"&gt;  &lt;tbody&gt;&lt;tr style=""&gt;   &lt;td style="border: 1pt solid windowtext; padding: 0in 5.4pt; width: 131.4pt;" valign="top" width="175"&gt;   &lt;p class="MsoNormal"&gt;Format48bppRgb&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 131.4pt;" valign="top" width="175"&gt;   &lt;p class="MsoNormal"&gt;Format64bppArgb&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 131.4pt;" valign="top" width="175"&gt;   &lt;p class="MsoNormal"&gt;Format64bppPArgb&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt;  &lt;p class="MsoNormal"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;Formats you can create a Bitmap with BUT can’t create a Graphics from the resulting Bitmap object:&lt;/p&gt;  &lt;table class="MsoTableGrid" style="border: medium none ; border-collapse: collapse;" border="1" cellpadding="0" cellspacing="0"&gt;  &lt;tbody&gt;&lt;tr style=""&gt;   &lt;td style="border: 1pt solid windowtext; padding: 0in 5.4pt; width: 131.4pt;" valign="top" width="175"&gt;   &lt;p class="MsoNormal"&gt;Format1bppIndexed&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 131.4pt;" valign="top" width="175"&gt;   &lt;p class="MsoNormal"&gt;Format4bppIndexed&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 131.4pt;" valign="top" width="175"&gt;   &lt;p class="MsoNormal"&gt;Format8bppIndexed&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 131.4pt;" valign="top" width="175"&gt;   &lt;p class="MsoNormal"&gt;Format16bppGrayScale&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr style=""&gt;   &lt;td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0in 5.4pt; width: 131.4pt;" valign="top" width="175"&gt;   &lt;p class="MsoNormal"&gt;Format16bppArgb1555&lt;/p&gt;   &lt;/td&gt;  &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt;  &lt;p class="MsoNormal"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style=""&gt;            &lt;/span&gt;There is one final thing to mention about drawing on specific formats, those formats being 16bppRgb555 and 16bppRgb565.&lt;span style=""&gt;   &lt;/span&gt;Colors in GDI+ are represented with 32 bits (Argb), 24 bits without Alpha (Rgb).&lt;span style=""&gt;  &lt;/span&gt;16bpp and 15bpp are clearly less than 24bpp.&lt;span style=""&gt;  &lt;/span&gt;So how do non-supported colors get drawn on these two formats?&lt;span style=""&gt;  &lt;/span&gt;Well the color is dithered.&lt;span style=""&gt;  &lt;/span&gt;You can read up on dithering and how it relates to images &lt;a href="http://en.wikipedia.org/wiki/Dithering#Digital_photography_and_image_processing"&gt;here&lt;/a&gt;. (just note that when that article talks about a color palette, it means every displayable color, it’s not used in the same sense as in this blog (we use it to mean a limited set of colors that bear no relation to the actual values in memory, they simply map to them)).&lt;span style=""&gt;  &lt;/span&gt;Basically, it creates the illusion of a color that cannot be displayed by interspersing two valid colors in a way that allows the human eye to interpret it as the un-displayable color (obviously you will need a group of the pixels to perform this operation, it can’t be done on single, invalid pixel values).&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style=""&gt;            &lt;/span&gt;You can always convert up in formats and not lose data.&lt;span style=""&gt;  &lt;/span&gt;When converting down you will not always lose data though.&lt;span style=""&gt;  &lt;/span&gt;In fact, when going from 32bppRgb to 24bppRgb you will lose nothing at all since the extra byte in 32bppRgb is unused.&lt;span style=""&gt;   &lt;/span&gt;When converting from a format that supports Alpha to a format that doesn’t support it, you will lose all Alpha data.&lt;span style=""&gt;  &lt;/span&gt;16bpp555 and 16bpp565 are unique, converting from one to the other will always lose color data (or have the potential to lose color data, they do share approx. 125 common colors).&lt;span style=""&gt;  &lt;/span&gt;To understand why, 11111-11111-11111 in 16bpp555 is not the same color as 11111-011111-11111 in 16bpp565.&lt;span style=""&gt;  &lt;/span&gt;True, they are the same numbers, but they do represent different colors (probably due to the fact that Microsoft wanted 16bpp565 to be able to display max red, max green, and max blue but there are only 5 bits for red and blue but 6 bits for green).&lt;span style=""&gt;  &lt;/span&gt;Try to picture it this way, the channel’s max will always be the true max for that color, and the channel’s min (0) will always be the complete absence of that color.&lt;span style=""&gt;  &lt;/span&gt;All other values can be computed by interpolating (think gradient) between all “true” colors and only picking enough colors to fill up all available space in the specific format (you would want these colors to be equally spaced too, wont you?).&lt;span style=""&gt;  &lt;/span&gt;So if you convert back and forth between 16bpp555 and 16bpp565, you will slowly make it down to only about 125 colors (this does not happen immediately because of dithering).&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style=""&gt;            &lt;/span&gt;Bitmaps are laid out in memory just about as you would expect.&lt;span style=""&gt;  &lt;/span&gt;To get at the memory of a Bitmap you will need to use the &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDrawingBitmapClassLockBitsTopic.asp"&gt;Bitmap.LockBits&lt;/a&gt; method to return a &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDrawingImagingBitmapDataClassTopic.asp"&gt;BitmapData&lt;/a&gt; object.&lt;span style=""&gt;  &lt;/span&gt;The &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDrawingImagingBitmapDataClassScan0Topic.asp"&gt;BitmapData.Scan0&lt;/a&gt; property points to the first channel of the first pixel.&lt;span style=""&gt;  &lt;/span&gt;In memory, the channel order is Bgr[A] A always comes last if it is supported by the format.&lt;span style=""&gt;  &lt;/span&gt;Don’t ask me why it’s backwards, I couldn’t explain it.&lt;span style=""&gt;  &lt;/span&gt;This continues until you reach the end of a row of pixels.&lt;span style=""&gt;  &lt;/span&gt;Rows are aligned at 4 byte boundaries; this means that unused bits are inserted at the end of each row if this alignment does not occur naturally.&lt;span style=""&gt;  &lt;/span&gt;That means that you can not find the address of the first channel in the second row of pixels by the formula StartAddress + Bitmap.Width * BytesPerPixel.&lt;span style=""&gt;  &lt;/span&gt;You may be off by one or more bytes.&lt;span style=""&gt;  &lt;/span&gt;Luckily, the &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDrawingImagingBitmapDataClassStrideTopic.asp"&gt;BitmapData.Stride&lt;/a&gt; property reports the real length of a single row of pixels memory wise.&lt;span style=""&gt;  &lt;/span&gt;Formats that do not encode channel in “byte lengths” (such as 16bpp555, 16bpp565, etc.) are harder to access.&lt;span style=""&gt;  &lt;/span&gt;You will need to use bit shifting operations to get at the value since the smallest memory value you can read at a time is one byte (eight bits).&lt;span style=""&gt;  &lt;/span&gt;In memory, the Bitmap may me laid top-to-bottom or bottom-to-top.&lt;span style=""&gt;  &lt;/span&gt;In practice, I have never come across an image that was actually in the bottom-to-top configuration but just know that it’s a possibility.&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style=""&gt;        &lt;/span&gt;One final note, the &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDrawingBitmapClassGetPixelTopic.asp"&gt;Bitmap.GetPixel&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDrawingBitmapClassSetPixelTopic.asp"&gt;Bitmap.SetPixel&lt;/a&gt; methods do all the memory magic behind the scenes for you and work correctly for every image format.&lt;span style=""&gt;  &lt;/span&gt;There is no problem with images that use a color palette since while in memory, it doesn’t use the palette and can display all of the colors that the non-indexed format can (even though the format is still 8bppIndexed for example).&lt;span style=""&gt;  &lt;/span&gt;The bit shifts necessary for getting and setting pixels in the 16bpp555 and 16bpp565 formats is done correctly.&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style=""&gt;            &lt;/span&gt;And that’s it!&lt;span style=""&gt;  &lt;/span&gt;Hopefully this post helped clarify any doubts you had in your mind about how GDI+ represents images.&lt;span style=""&gt;  &lt;/span&gt;It’s a lot of information to keep in mind but most of it is intuitive, and all of it is helpful when dealing with the internals of an image.&lt;/p&gt; &lt;p class="MsoNormal"&gt;As a bonus, here is what happens when you try and work with some of the unsupported formats:&lt;br /&gt;&lt;/p&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td valign="top"&gt;Original, After Saving as 48bppRgb Bitmap, After Recovery Attempt&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://photos1.blogger.com/blogger/5834/1751/1600/Restored%20Picture.0.jpg"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer;" src="http://photos1.blogger.com/blogger/5834/1751/400/Restored%20Picture.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td valign="top"&gt;After trying to draw a Black line with Width 1 on a 64bppArgb Bitmap&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://photos1.blogger.com/blogger/5834/1751/1600/64bppArgb.jpg"&gt;&lt;img style="cursor: pointer;" src="http://photos1.blogger.com/blogger/5834/1751/400/64bppArgb.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/24915814-114420211348079889?l=bmpinroad.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bmpinroad.blogspot.com/feeds/114420211348079889/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=24915814&amp;postID=114420211348079889' title='35 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/24915814/posts/default/114420211348079889'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/24915814/posts/default/114420211348079889'/><link rel='alternate' type='text/html' href='http://bmpinroad.blogspot.com/2006/04/file-formats-pixel-formats.html' title='File Formats, Pixel Formats'/><author><name>colithium</name><uri>http://www.blogger.com/profile/03625900788586728203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>35</thr:total></entry></feed>
