Archive for the Software Category

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. }

After a little work getting the page to match the rest of the website, I’ve finally put up my source code to display text on an LCD screen in 4-bit mode using a microcontroller. It isn’t earth shattering technology, but is a good start for anyone looking to do something fun with embedded microprocessors and is also a useful for debugging the integration process of sensors and actuation for robotics.

The source, pictures, datasheets, and explanation can be found by following the project page links at the side of the page or simply going to the 4-bit LCD.