2008-12-08 36 views
1

我正在尝试编写一个将每像素48位PNG文件转换为专有(拜耳)格式的应用程序。我如何查看'原始'PNG图像数据

下面的代码(礼貌here)适用于某些PNG文件格式,但是当我尝试一个真正的48位PNG时,代码会抛出异常 - 是否有替代方法?

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)); 
     } 
    } 
+0

也许你想告诉我们异常的文本? – 2008-12-08 20:23:22

回答

4

BMP编码器不支持48bpp格式。使用Bitmap.LockBits()方法可以在像素上获得裂缝。虽然在MSDN Library文章的P​​ixelFormat说,48bpp就像24 bpp的图像处理,我其实确实看到6字节的像素与此代码:

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); 
+0

非常感谢您的工作。 – Jamie 2008-12-09 17:58:28

相关问题