Mat*_*ond 7 c# bitmapsource pixelformat
我使用以下内容将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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何得到PixelFormat它BitmapSource,所以我的图像被破坏了.
对于上下文,我使用这种技术,因为我想加载一个tiff,可能是8或16灰色或24或32位颜色,我需要PixelFormat保留.我更喜欢修复ConvertBitmapSourceToBitmap它,因为它相当方便,但也很乐意用更好的技术替换以下代码,从BitmapSource创建一个Bitmap:
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]);
}
Run Code Online (Sandbox Code Playgroud)