Question

J'essaie d'écrire une application qui convertit les fichiers PNG 48 bits par pixel en un format propriétaire (Bayer).

Le code (avec la permission ici ) ci-dessous fonctionne très bien pour certains formats de fichiers PNG, mais lorsque j'essaie un fichier PNG 48 bits authentique, le code lève une exception. Existe-t-il une 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));
        }
    }
Était-ce utile?

La solution

L'encodeur BMP ne prend pas en charge les formats 48bpp. Vous pouvez obtenir une fissure au niveau des pixels avec la méthode Bitmap.LockBits (). Bien que l'article de la bibliothèque MSDN pour PixelFormat indique que 48bpp est traité comme des images 24bpp, je vois en fait des pixels de 6 octets avec ce 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);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top