By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,870 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,870 IT Pros & Developers. It's quick & easy.

.NET doesn’t recognize PixelFormat.Format16bppRgb565

P: 4
I am writing a regression testing suite that uses some image comparison features for some RFI (radio frequency interference) testing equipment. I capture an image (from the screen of the device), and save this first image as a reference as “what the screen should look like;” then when executing a “macro” on the device, I will then capture another image and compare it to the “reference” image – the details aren’t important, but FYI.

The images I am analyzing come to me in a bitmaped (800X600) image that uses PixelFormat.Format16bppRgb565.

The problem is that I need to save an image of this pixel format to disk, then retrieve it later for comparison. When I attempt to open this saved bmp, .NET (I have tried both VB and C#) open it as a PixelFormat.Format32bppRgb (automatically converting it for me) – and I need the image to be in the same PixelFormat to compare them.

So, I have a couple of possible solutions:
1) read the saved bitmap (in PixelFormat.Format16bppRgb565) raw off the hard disk using a BinaryReader. – The problem with that is that BMP images have header files that make the bmp load correctly; if one were to read the raw bmp data off the hard disk, it is usually flipped 180, inverted, and slightly offset.
2) Use some .NET library to correctly open the bitmapped image (PixelFormat.Format16bppRgb565).
3) Convert both images (the one from disk, and the newly captured image) into a compatible format. Ideally PixelFormat.Format8bppIndexed (or .gif). I messed around with this for a while trying to do something like:
Expand|Select|Wrap|Line Numbers
  1.         public static Bitmap ConvertToGif(Bitmap img) {
  2.             Bitmap temp = new Bitmap(img.Width, img.Height, 
  3.                    PixelFormat.Format8bppIndexed);
  4.             BitmapData tempLocked = temp.LockBits(
  5.                 new Rectangle(0, 0, temp.Width, temp.Height),
  6.     ImageLockMode.WriteOnly,     PixelFormat.Format8bppIndexed);
  7.             Byte[] a = new Byte[img.Width * img.Height];
  8.             temp.UnlockBits(tempLocked);
  9.  
  10.             MemoryStream fs = new MemoryStream(a);
  11.             img.Save(fs, ImageFormat.Gif);
  12.             temp = new Bitmap(fs);
  13.             fs.Close();
  14.  
  15.             return temp;
  16. }
  17.  
With no luck – any ideas?
Aug 30 '07 #1
Share this Question
Share on Google+
1 Reply


P: 4
I actually figured out how to read the piselformat.16rbg565 in c#


Expand|Select|Wrap|Line Numbers
  1. private const int BITMAP_HEADER_OFFSET_BYTES = 61;
  2.  
  3. public static Bitmap Open565(String fileName) {
  4.             try {
  5.                 Bitmap temp = new Bitmap(800, 600, PixelFormat.Format16bppRgb565);
  6.                 BitmapData tempLocked = temp.LockBits(
  7.                     new Rectangle(0, 0, 800, 600), ImageLockMode.WriteOnly,
  8.                     PixelFormat.Format16bppRgb565);
  9.                 BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open));
  10.                 byte[] bin = new byte[tempLocked.Height * tempLocked.Stride + 
  11.                     BITMAP_HEADER_OFFSET_BYTES];
  12.  
  13.                 br.Read(bin, 0, bin.Length - 1);
  14.                 br.Close();
  15.  
  16.                 System.Runtime.InteropServices.Marshal.Copy(bin,
  17.                     BITMAP_HEADER_OFFSET_BYTES - 1, tempLocked.Scan0,
  18.                     bin.Length - 1 - BITMAP_HEADER_OFFSET_BYTES);
  19.  
  20.                 temp.UnlockBits(tempLocked);
  21.                 temp.RotateFlip(RotateFlipType.RotateNoneFlipY);
  22.                 return temp;
  23.             } catch (Exception) {
  24.                 Console.WriteLine("Error in converting image.");
  25.                 return new Bitmap(1, 1, PixelFormat.Format4bppIndexed);
  26.             }
  27.  
  28.  
  29.         }
  30.  
does anyone know how to convert an image to .gif (PixelFormat.Format8bppIndexed) in runtime?

thanks
Aug 30 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.