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:
-
public byte[] GetByteArrayFromBitmap(Bitmap TheBMP)
-
{
-
// First, create a memory a stream where
-
// the passed bitmap will be stored
-
MemoryStream BitmapMS = new MemoryStream();
-
-
// Now save the bitmap into the stream
-
TheBMP.Save(BitmapMS, Imaging.ImageFormat.Bmp);
-
-
// Create a byte array that is the same size as the
-
// memory stream that houses the recently saved bitmap
-
byte[] BMPArray = new byte[BitmapMS.Length];
-
-
// Store the contents of the memory
-
// stream into byte array
-
BMPArray = BitmapMS.ToArray();
-
-
// Finally, return the byte array
-
return BMPArray;
-
}
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.
-
public Bitmap GetBitmapFromByteArray(byte[] BMPArray)
-
{
-
// First, create a memory a stream where the
-
// passed byte array will be stored
-
MemoryStream BitmapMS = new MemoryStream();
-
-
// Write the byte array to the memory stream
-
BitmapMS.Write(BMPArray, 0, BMPArray.Length);
-
-
// Create a new bitmap object from the memory stream
-
Bitmap TheBitmap = new Bitmap(BitmapMS);
-
-
// Finally, return the bitmap
-
return TheBitmap;
-
}
Entries (RSS)
September 21st, 2007 at 09:16 am
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
June 11th, 2008 at 01:36 pm
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
August 7th, 2008 at 02:42 am
Hi,
I tried ur solution but i am getting error at Bitmap TheBitmap = new Bitmap(BitmapMS);
as Parameter is not valid.
What to do?