On my latest project I found a need to use Bitmap images in my .NET Compact Framework application. Using Visual Studio 2005 (Beta 2) and the Windows Mobile 5 SDK the application is currently stable on an HP iPAQ running WM5. There are a few things that annoy me about developing on this platform, though, because the Compact Framework 2.0 is still buggy and the API is not well documented nor easy to find on the MSDN site.

I wanted to create a data file that could contain metadata and images so that I didn’t have a mess of extra files hanging around on the PDA. Using XML I want to tag profiles so that meaningful and easy to read data along with an embedded picture can be saved. For understanding purposes the meaningful data would be an email address and the image would be a bitmap. Saving is easy and can be done using the following code:

  1. public byte[] GetByteArrayFromBitmap(Bitmap TheBMP)
  2. {
  3. //  First, create a memory a stream where
  4. //  the passed bitmap will be stored
  5. MemoryStream BitmapMS = new MemoryStream();
  6.  
  7. //  Now save the bitmap into the stream
  8. TheBMP.Save(BitmapMS, Imaging.ImageFormat.Bmp);
  9.  
  10. //  Create a byte array that is the same size as the
  11. //  memory stream that houses the recently saved bitmap
  12. byte[] BMPArray = new byte[BitmapMS.Length];
  13.  
  14. //  Store the contents of the memory
  15. //  stream into byte array
  16. BMPArray = BitmapMS.ToArray();
  17.  
  18. //  Finally, return the byte array
  19. return BMPArray;
  20. }

In the above code I transform the bitmap into a byte array (byte[]) using the MemoryStream class. The length of the byte array is stored to the XML file and then the entire byte array is written. Again, this is the easy part. The reverse — turning the byte array into a bitmap image — is not necessarily difficult but it took a while to find.

  1. public Bitmap GetBitmapFromByteArray(byte[] BMPArray)
  2. {
  3. //  First, create a memory a stream where the
  4. //  passed byte array will be stored
  5. MemoryStream BitmapMS = new MemoryStream();
  6.  
  7. //  Write the byte array to the memory stream
  8. BitmapMS.Write(BMPArray, 0, BMPArray.Length);
  9.  
  10. //  Create a new bitmap object from the memory stream
  11. Bitmap TheBitmap = new Bitmap(BitmapMS);
  12.  
  13. //  Finally, return the bitmap
  14. return TheBitmap;
  15. }

3 Responses to “Working With Bitmaps in the .NET Compact Framework”

  1. Mario says:

    Hello,

    I’m using the same DB used in northwindoledb example, i need a similar app but in C#, not C++, so i use this DB and make a form for Windows Mobile 6 Emulator in Vista (device application) with visual studio 2005, i use a function to recover the image and display in a PictureBox but when this image create a BitMap give me an error “Value does not fall within the expected range.”, i seek a lot in internet and don’t find a solution, the function the same than you publish, If you can help me i will appreciate

  2. Clive W. Humphris says:

    Hello,

    I hope you may be able to help

    I’m following the instructions to make a bitmap, which is to be copied into a picturebox.

    The content of the bitmap is drawn using the line and circle controls (size 705,409) is perfect with coloured lines, text etc.

    My problem is that it has a black background, but only in the .NET Compact Framework. Using a similar technique in VB2005 and the bit map is perfect, with a white background, exactly as it should be.

    Any guidance would be greatly appreciated.

    Kind regards

    Clive

  3. Jayesh says:

    Hi,

    I tried ur solution but i am getting error at Bitmap TheBitmap = new Bitmap(BitmapMS);
    as Parameter is not valid.

    What to do?

Leave a Reply