我用了
private BitmapImage byteArrayToImage(byte[] byteArrayIn)
{
try
{
MemoryStream stream = new MemoryStream();
stream.Write(byteArrayIn, 0, byteArrayIn.Length);
stream.Position = 0;
System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
BitmapImage returnImage = new BitmapImage();
returnImage.BeginInit();
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
returnImage.StreamSource = ms;
returnImage.EndInit();
return returnImage;
}
catch (Exception ex)
{
throw ex;
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序中的此方法将字节数组转换为图像.但它抛出"参数无效"异常..为什么会发生..?有没有其他方法.??
我有一个System.Drawing.Image的实例.
如何在我的WPF应用程序中显示这个?
我尝试过,img.Source但这不起作用.
我正在尝试将字节数组从我的数据库绑定到WPF图像.
我的XAML:
<Window.Resources>
<local:BinaryImageConverter x:Key="imgConverter" />
</Window.Resources>
...
<Image Source="{Binding Path=ImageData, Converter={StaticResource imgConverter}}" />
Run Code Online (Sandbox Code Playgroud)
我修改了Ryan Cromwell发布的代码转换代码:
Class BinaryImageConverter
Implements IValueConverter
Private Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
If value IsNot Nothing AndAlso TypeOf value Is Byte() Then
Dim bytes As Byte() = TryCast(value, Byte())
Dim stream As New MemoryStream(bytes)
Dim image As New BitmapImage()
image.BeginInit()
image.StreamSource = stream
image.EndInit()
Return image
End If …Run Code Online (Sandbox Code Playgroud)