我想转换指纹字节数组到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'包含所需的图像头?例如你试图保存到PNG,但它有PNG文件头? –
@EmrahSüngü不,它不包含任何标题。如果它应该包含,我如何将图像头部分配给字节[] fp_image –
Winforms System.Drawing.Bitmap的所有代码都没有意义。如果'fp_image'包含有效的位图帧(编码为PNG或JPEG),则可以直接将其传递给MemoryStream构造函数,并将该流分配给WPF BitmapImage的'StreamSource'属性。如果'fp_image'是一个原始像素缓冲区,则需要知道位图的像素格式,宽度和高度,并将这些值传递给BitmapSource.Create()重载之一。 – Clemens