将 Image.Source 绑定到 WPF 中的字符串? [英] Binding Image.Source to String in WPF?
本文介绍了将 Image.Source 绑定到 WPF 中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下 XAML 代码:
I have below XAML code :
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
WindowStartupLocation="CenterScreen"
Title="Window1" Height="300" Width="300">
<Grid>
<Image x:Name="TestImage" Source="{Binding Path=ImageSource}" />
</Grid>
</Window>
此外,还有一种方法可以从 Base64
字符串生成图像:
Also, there is a method that makes an Image from a Base64
string :
Image Base64StringToImage(string base64ImageString)
{
try
{
byte[] b;
b = Convert.FromBase64String(base64ImageString);
MemoryStream ms = new System.IO.MemoryStream(b);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
//////////////////////////////////////////////
//convert System.Drawing.Image to WPF image
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(img);
IntPtr hBitmap = bmp.GetHbitmap();
System.Windows.Media.ImageSource imageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
Image wpfImage = new Image();
wpfImage.Source = imageSource;
wpfImage.Width = wpfImage.Height = 16;
//////////////////////////////////////////////
return wpfImage;
}
catch
{
Image img1 = new Image();
img1.Source = new BitmapImage(new Uri(@"/passwordManager;component/images/TreeView/empty-bookmark.png", UriKind.Relative));
img1.Width = img1.Height = 16;
return img1;
}
}
现在,我要将 TestImage
绑定到 Base64StringToImage
方法的输出.
我使用了以下方式:
Now, I'm gonna bind TestImage
to the output of Base64StringToImage
method.
I've used the following way :
public string ImageSource { get; set; }
ImageSource = Base64StringToImage("iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAABjUExURXK45////6fT8PX6/bTZ8onE643F7Pf7/pDH7PP5/dns+b7e9MPh9Xq86NHo947G7Hm76NTp+PL4/bHY8ojD67rc85bK7b3e9MTh9dLo97vd8/D3/Hy96Xe76Nfr+H+/6f///1bvXooAAAAhdFJOU///////////////////////////////////////////AJ/B0CEAAACHSURBVHjaXI/ZFoMgEEMzLCqg1q37Yv//KxvAlh7zMuQeyAS8d8I2z8PT/AMDShWQfCYJHL0FmlcXSQTGi7NNLSMwR2BQaXE1IfAguPFx5UQmeqwEHSfviz7w0BIMyU86khBDZ8DLfWHOGPJahe66MKe/fIupXKst1VXxW/VgT/3utz99BBgA4P0So6hyl+QAAAAASUVORK5CYIII").Source.ToString();
但什么也没发生.
我该如何解决?
but nothing happen.
How can I fix it ?
顺便说一句,我很确定 base64 字符串是正确的
BTW, I'm dead sure that the base64 string is correct
推荐答案
作为对@itowlson 出色答案的补充,您的代码应该如下所示:
As a complement to @itowlson's excellent answer, this is what your code should look like:
// MainWindow.xaml
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<DockPanel>
<Image Source="{Binding ImageSource}" />
</DockPanel>
</Window>
// MainWindow.xaml.cs
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var model = new MainModel();
DataContext = model;
model.SetImageData(File.ReadAllBytes(@"C:UsersPublicPicturesSample PicturesDesert.jpg"));
}
}
class MainModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void SetImageData(byte[] data) {
var source = new BitmapImage();
source.BeginInit();
source.StreamSource = new MemoryStream(data);
source.EndInit();
// use public setter
ImageSource = source;
}
ImageSource imageSource;
public ImageSource ImageSource
{
get { return imageSource; }
set
{
imageSource = value;
OnPropertyChanged("ImageSource");
}
}
protected void OnPropertyChanged(string name)
{
var handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
这篇关于将 Image.Source 绑定到 WPF 中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文