2008-12-08 179 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