Domanda

Sto provando a scrivere un'applicazione che converte file PNG a 48 bit per pixel in un formato proprietario (Bayer).

Il codice (per gentile concessione qui ) di seguito funziona benissimo per alcuni formati di file PNG, ma quando provo un PNG in buona fede a 48 bit il codice genera un'eccezione: esiste un'alternativa?

    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));
        }
    }
È stato utile?

Soluzione

Il codificatore BMP non supporta i formati 48bpp. È possibile ottenere una crepa nei pixel con il metodo Bitmap.LockBits (). Sebbene l'articolo della libreria MSDN per PixelFormat affermi che 48bpp è trattato come immagini 24bpp, in realtà vedo pixel a 6 byte con questo codice:

  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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top