Domanda

Sto usando quanto segue per convertire a BitmapSource a a Bitmap:

internal static Bitmap ConvertBitmapSourceToBitmap(BitmapSource bitmapSrc)
{
    int width = bitmapSrc.PixelWidth;
    int height = bitmapSrc.PixelHeight;
    int stride = width * ((bitmapSrc.Format.BitsPerPixel + 7) / 8);

    byte[] bits = new byte[height * stride];

    bitmapSrc.CopyPixels(bits, stride, 0);

    unsafe
    {
        fixed (byte* pBits = bits)
        {
            IntPtr ptr = new IntPtr(pBits);

            return new System.Drawing.Bitmap(
                width,
                height,
                stride,
                System.Drawing.Imaging.PixelFormat.Format32bppPArgb, //The problem
                ptr);
        }
    }
}

Ma non so come ottenere il PixelFormat del BitmapSource, quindi le mie immagini sono mangeli.

Per il contesto, sto usando questa tecnica perché voglio caricare una TIFF, che potrebbe essere di 8 o 16 grigi o a 24 o 32 bit, e ho bisogno del PixelFormat da preservare. Preferirei correggere il mio ConvertBitmapSourceToBitmap Dato che è piuttosto utile, ma sarebbe anche felice di sostituire il seguente codice con una tecnica migliore per la creazione di una bitmap da un bitmapsource:

Byte[] buffer = File.ReadAllBytes(filename.FullName);
using (MemoryStream stream = new MemoryStream(buffer))
{
    TiffBitmapDecoder tbd = new TiffBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);

    return BitmapBitmapSourceInterop.ConvertBitmapSourceToBitmap(tbd.Frames[0]);
}
È stato utile?

Soluzione

Qualcosa di sbagliato nell'uso Bitmapsource.format? Questo è il pixelformat e lo stai già usando per determinare il passo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top