BitMapsourceのPixelformatを取得するにはどうすればよいですか
-
28-10-2019 - |
質問
次のものを使用してaを変換しています BitmapSource
に 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);
}
}
}
しかし、私はどのように取得するかわかりません PixelFormat
の BitmapSource
, 、だから私の画像は壊れています。
コンテキストのために、私はこの手法を使用しています。なぜなら、私はティフをロードしたいからです。 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、そしてあなたはすでにそれを使用して歩みを決定しています。
所属していません StackOverflow