質問

次のものを使用してaを変換しています BitmapSourceBitmap:

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);
        }
    }
}

しかし、私はどのように取得するかわかりません PixelFormatBitmapSource, 、だから私の画像は壊れています。

コンテキストのために、私はこの手法を使用しています。なぜなら、私はティフをロードしたいからです。 PixelFormat 保存されます。修正したいです ConvertBitmapSourceToBitmap かなり便利ですが、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]);
}
役に立ちましたか?

解決

使用が問題なことは何でも bitmapsource.format?これ Pixelformat、そしてあなたはすでにそれを使用して歩みを決定しています。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top