How to view "raw" PNG image data

I am trying to write an application that converts 48-bit PNG files to native (Bayer) format.

The code (courtesy of here ) below works great for some PNG file formats, but when I try to bona fide the 48-bit PNG the code throws an exception - is there an alternative?

    static public byte[] BitmapDataFromBitmap(Bitmap objBitmap)
    {
        MemoryStream ms = new MemoryStream();
        objBitmap.Save(ms, ImageFormat.BMP);  // GDI+ exception thrown for > 32 bpp
        return (ms.GetBuffer());
    }

    private void Bayer_Click(object sender, EventArgs e)
    {
        if (this.pictureName != null)
        {
            Bitmap bmp = new Bitmap(this.pictureName);
            byte[] bmp_raw = BitmapDataFromBitmap(bmp);
            int bpp = BitConverter.ToInt32(bmp_raw, 28); // 28 - BMP header defn.

            MessageBox.Show(string.Format("Bits per pixel = {0}", bpp));
        }
    }

      

+1


source to share


1 answer


BMP encoder does not support 48bpp formats. You can get crack in pixels with Bitmap.LockBits () method. Although the MSDN Library article for PixelFormat says 48bpp is treated as 24bpp images, I do see 6 byte pixels with this code:



  Bitmap bmp = new Bitmap(@"c:\temp\48bpp.png");
  BitmapData bd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
    ImageLockMode.ReadOnly, PixelFormat.Format48bppRgb);
  // Party with bd.Scan0
  //...
  bmp.UnlockBits(bd);

      

+4


source







All Articles