指纹字节数组到 WPF 中的图像源



我正在尝试在WPF应用程序中将指纹字节数组转换为图像源,但我的技巧不起作用。 这是代码:

byte[] fp_image;  //fp_image contains the fingerprint byte array result 
using (MemoryStream ms = new MemoryStream(fp_image))
{
Bitmap bmp = new Bitmap(ms);
FpImage.Source = ConvertToImageSource(bmp);
}
public static ImageSource ConvertToImageSource(Bitmap bitmap)
{
var imageSourceConverter = new ImageSourceConverter();
using (var memoryStream = new MemoryStream())
{
bitmap.Save(memoryStream, ImageFormat.Png);
var snapshotBytes = memoryStream.ToArray();
return (ImageSource)imageSourceConverter.ConvertFrom(snapshotBytes); ;
}
}

注意:如果我将任何其他图像转换为字节数组并使用此代码将其转换回图像,它就可以工作。

假设byte[] fp_image包含原始数据,那么您可以使用如下所示的方法直接创建一个BitmapSource(这是ImageSource的子类(。但是,您需要知道缓冲区中位图的宽度、高度和像素格式。

public BitmapSource GetBitmapFromRawBytes(
byte[] rawBytes, int width, int height, PixelFormat format)
{
var stride = (width * format.BitsPerPixel + 7) / 8;
return BitmapSource.Create(width, height, 96, 96, format, null, rawBytes, stride);
}

最新更新