Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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/3/apache-spark/5.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
如何从字符串数据设置wpf图像源?_Wpf - Fatal编程技术网

如何从字符串数据设置wpf图像源?

如何从字符串数据设置wpf图像源?,wpf,Wpf,我在cs代码中从mydatabase中mytable的[Image]列中得到一个字符串值,类型为varbinaryMax,现在我想将该值设置为图像控件的源 我编写了以下代码,但未将图像设置为源代码: string strImage = "0xFFD8FFE000104A46494600010....." static byte[] GetBytes(string str) { byte[] bytes = new byte[str.Length * sizeof(char)];

我在cs代码中从mydatabase中mytable的[Image]列中得到一个字符串值,类型为varbinaryMax,现在我想将该值设置为图像控件的源

我编写了以下代码,但未将图像设置为源代码:

string strImage = "0xFFD8FFE000104A46494600010....."

static byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}
在构造函数中:

var bytes = GetBytes(strImage);
var myImage = new MyImageModel {Content = bytes};
MyImg.DataContext = myImage;
在Xaml中:

<Image x:Name="MyImg" Source="{Binding Path=Content}"/>     
上述代码不起作用,也不是错误


要将字符串代码转换为图像文件,请使用如下转换

public BitmapImage Base64ToImage( byte[] byteArray)
{
  byteArray;  // your bytes
                        BitmapImage img = new BitmapImage();
                        using (MemoryStream stream = new MemoryStream(byteArray))
                        {
                            img.BeginInit();
                            img.StreamSource = stream;
                            img.CacheOption = BitmapCacheOption.OnLoad;
                            img.EndInit();
                            img.Freeze();
                        }
                       return img;
}
它将为您提供一个图像您可以直接将此图像绑定到UI

您可以使用转换器并将字节字符串绑定到图像的源属性。所以内容就是你的字节字符串

<converters:BytesToImageConverter x:Key="BytesToImageConverter" />
<Image x:Name="MyImg" Source="{Binding Path=Content, Converter={StaticResource BytesToImageConverter}}"/>     

你正在为你的内容属性实现PropertyChanged吗?@Krishna这在这里是不必要的。二进制数据的实际格式是什么?它是编码图像缓冲区吗?它是如何编码的?@Clemens我的问题与你的问题不一样,那就是从strImage到包含有效编码图像缓冲区的字节数组的转换。你还没有告诉我们任何关于编码的事情。在调试器中运行应用程序时,请查看Visual Studio中的输出窗口。您应该会在那里看到一条数据绑定错误消息,这是由于转换失败造成的。请注意,问题是关于WPF,而不是WinForms。您的方法返回System.Drawing.Image,它不能直接在WPF中使用。还不清楚这个问题是否是关于base64编码的图像。嗨,Dinesh,我以前试过这个方法,但出现以下错误:找不到适合完成此操作的成像组件。@Mohsen是您的base64编码图像吗?@Clemens是的,您是对的。现在我更新了代码。@Mohsen现在我更新了答案,请尝试此项。我一直想知道,当这是默认值时,为什么人们会将BitmapImage的UriSource属性设置为null无论如何。@Clemens只是想确定一下?:还是一种习惯…确定什么?您之前不是创建了BitmapImage实例吗?也就是说,您实际上不需要转换器。您可以直接将Image.Source绑定到byte[]属性,就像OP一样。内置类型转换将完成其余操作。@Loetn,现在使用您的方法,但在line image.EndInit中出现以下错误:Exception>未找到适合完成此操作的成像组件。和innerException>找不到该组件。来自HRESULT的异常:0x88982F50
   public class BytesToImageConverter : IValueConverter
   {
       public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
       {
           if (value == null)
               return null;

           if (!(value is byte[]))
               throw new ArgumentException("Only byte[] values are supported.");

           if (((byte[])value).Length == 0)
               return null;

           // return new ImageSourceConverter().ConvertFrom(value);
           BitmapImage image = new BitmapImage();
           using (MemoryStream ms = new MemoryStream((byte[])value))
           {
               ms.Position = 0;
               image.BeginInit();
               image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
               image.CacheOption = BitmapCacheOption.OnLoad;
               image.UriSource = null;
               image.StreamSource = ms;
               image.EndInit();
           }
           image.Freeze();

           return image;
       }

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