Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WPF中读取base64图像?_C#_.net_Wpf - Fatal编程技术网

C# 如何在WPF中读取base64图像?

C# 如何在WPF中读取base64图像?,c#,.net,wpf,C#,.net,Wpf,我知道如何在WinForms中完成它 byte[] binaryData = Convert.FromBase64String(bgImage64); image = Image.FromStream(new MemoryStream(binaryData)); 但是我如何在WPF中做同样的事情呢?在上面Josh的答案的基础上进行扩展-这里有一个使用ValueConverter的方法,因此您可以使用绑定,而不是在后面的代码中设置图像源 byte[] binaryData = Convert.F

我知道如何在WinForms中完成它

byte[] binaryData = Convert.FromBase64String(bgImage64);
image = Image.FromStream(new MemoryStream(binaryData));

但是我如何在WPF中做同样的事情呢?

在上面Josh的答案的基础上进行扩展-这里有一个使用ValueConverter的方法,因此您可以使用绑定,而不是在后面的代码中设置图像源

byte[] binaryData = Convert.FromBase64String(bgImage64);

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = new MemoryStream(binaryData);
bi.EndInit();

Image img = new Image();
img.Source = bi;
Base64ImageConverter.cs

public class Base64ImageConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string s = value as string;

        if (s == null)
            return null;

        BitmapImage bi = new BitmapImage();

        bi.BeginInit();
        bi.StreamSource = new MemoryStream(System.Convert.FromBase64String(s));
        bi.EndInit();

        return bi;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
ImageData.cs(用作数据源)

Window1.xaml

<Window x:Class="ImageFromData.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ImageFromData"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <local:Base64ImageConverter x:Key="Base64ImageConverter"/>
        <local:ImageData x:Key="ImageData"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource ImageData}">
        <Image Source="{Binding Base64ImageData, Converter={StaticResource Base64ImageConverter}}"/>
    </Grid>
</Window>


(项目的根命名空间是ImageFromData)

在我遇到的一种情况下,我创建了一个转换器来处理Base64字符串到WPF图像的转换。因此,您可以将其绑定到base64字符串属性,并让转换器处理其余部分

   public class ByteToImageConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if(string.IsNullOrEmpty(value.ToString())return;
                var imgBytes = Convert.FromBase64String(value.ToString());
                if (imgBytes == null)
                    return null;
                using (var stream = new MemoryStream(imgBytes))
                {
                    return BitmapFrame.Create(stream,
                        BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                }
            }

            public object ConvertBack(object value, Type targetType, object parameter,
                System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

这两行代码中的哪一部分在WPF中不可用?WPF不像winforms那样使用System.Drawing.Bitmap图像。我想问题应该是“如何从一个blob加载WPF图像”?Orion:回去工作吧。我将在我的应用程序中查看我是如何做到这一点的。几个月前我才涉猎过WPF。它用什么来制作图像?只是在谷歌上快速搜索了一下。看看这是否有帮助:(我不是把这作为一个答案,因为就像我说的,我对WPF知之甚少,也不知道这是否有效)谢谢!!!我一直在努力将base64字符串转换为图像并显示在页面上。我可以做转换,但我无法让它显示图像。这正是我需要的!不要忘记添加
bi.CacheOption=BitmapCacheOption.OnLoad
before
bi.EndInit()
如果您想在设置
bi.StreamSource
后立即关闭
MemoryStream
   public class ByteToImageConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if(string.IsNullOrEmpty(value.ToString())return;
                var imgBytes = Convert.FromBase64String(value.ToString());
                if (imgBytes == null)
                    return null;
                using (var stream = new MemoryStream(imgBytes))
                {
                    return BitmapFrame.Create(stream,
                        BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                }
            }

            public object ConvertBack(object value, Type targetType, object parameter,
                System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }