【问题标题】:How do I view 'raw' PNG image data如何查看“原始”PNG 图像数据
【发布时间】:2010-09-25 22:19:21
【问题描述】:

我正在尝试编写一个将每像素 48 位 PNG 文件转换为专有 (Bayer) 格式的应用程序。

下面的代码(由 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));
        }
    }

【问题讨论】:

  • 也许您想告诉我们异常的文本?

标签: .net png c#-2.0 bmp


【解决方案1】:

BMP 编码器不支持 48bpp 格式。您可以使用 Bitmap.LockBits() 方法对像素进行破解。虽然 PixelFormat 的 MSDN 库文章说 48bpp 被视为 24bpp 图像,但我确实看到这段代码是 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多