我使用以下代码将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);
}
}
}但是我不知道如何获取BitmapSource的PixelFormat,所以我的图像被损坏了。
对于上下文,我使用这种技术是因为我想加载一个tiff,它可能是8或16灰色,或者24或32位颜色,并且我需要保留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]);
}发布于 2011-06-17 03:53:09
使用BitmapSource.Format有什么问题吗?这是PixelFormat,您已经在使用它来确定步幅。
https://stackoverflow.com/questions/6373762
复制相似问题