Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

Writing to a bimap and saving it, makes it unusable


I start with a bitmap which is readable by MapPoint.  The variable name of the bitmap (defined earlier) is called "fileName".  Then I write the digit "1" on it, and save the file.  The subsequent bitmap can not be added to a MapPoint symbol's list.  It produces the following exception:

"The format of this bitmap is not supported.  Bitmaps must be uncompressed and use 1, 4, 8 or 24 bit color".

Can anyone see in my code what I am doing wrong?


Image img = Image.FromFile(fileName);
Bitmap bmp = new Bitmap(img);
// get a graphics so we can draw
Graphics g = Graphics.FromImage(bmp2);
Font arial            = new Font( "Arial", 7 );
Font arialBold      = new Font( arial, FontStyle.Bold );

g.DrawString("1", arialBold, Brushes.White, new PointF(3,1));

// release the graphics

// save the changes in the original fileName
bmp.Save( "newVersion.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
  • 2
  • 2
3 Solutions
If you create an Image first, then a bitmapfrom this image, the 'Bit Depth' for the bmp object will be 32 bit (check 'PixelFormat' property). Try creating the bitmap directly from the fileName:

Bitmap bmp = new Bitmap(fileName);
Graphics g = Graphics.FromImage(bmp);
ba272Author Commented:
No, that didn't work. But I think I found a symptom of the problem.  When I drag and drop the non-working bitmap into VS 2003, I get an error.  It says it can't open the bitmap because there are greater that 256 colors.  So I presume my use of Brushes.White is the problem.

Any idea how to get around this?

Mike TomlinsonMiddle School Assistant TeacherCommented:
You can specify the PixelFormat of the Bitmap when you create it, but you then can't start with the Image in it:

After creating the Bitmap and getting a Graphics for it, you can draw the original image into it using the Graphics.DrawImage() function:

When you create a bitmap giving the file name of .bmp file as parameter, the new created image will have the same color depth. So if the new image has more than 256 color this means that the original bitmap has more than 256 colors too.
 256 colors correspond to 8 Bit Depth, and a value of 'Format8bppIndexed' for PixelFormat property. (as a note, you will cannot create Graphics object from this kind of bitmap).

VS2003 should open bitmap files even with they have more than 256 colors. You say that the original can be opened and the new one can't?
ba272Author Commented:
Okay.  So now it seems THE question is how do I create the new bitmap.  I'll be drawing the original image into the new bitmap once it's created.  But which of 12 constructors do I use when I create it?

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now