主要参考https://www.cnblogs.com/lunawzh/p/6694082.html
其他参考https://blog.csdn.net/qq_36242487/article/details/79172895
https://bbs.csdn.net/topics/370258603
本博客将提供几种方法说明Image控件 source 的绑定方式。
<Image x:Name="BGimage" Source="xx/xx/Images/item.png" Height="15" Width="15" />
或
/// <summary> /// 测试曝光状态 /// </summary> /// <param name="str"></param> private void TestUpdateExposeStatus(string str) { string strImagePath = null; App.Current.Dispatcher.Invoke((Action)delegate () { switch (str) { case "0": strImagePath = "Icons/StudyPage/Exposure.png"; break; case "1": strImagePath = "Icons/StudyPage/预曝光.png"; break; case "2": strImagePath = "Icons/StudyPage/正在曝光.png"; break; case "3": strImagePath = "Icons/StudyPage/禁止曝光.png"; break; default: break; } this.img_ExposeStatus.Source = new BitmapImage(new Uri(strImagePath, UriKind.Relative)); }); }
或者
<Image Width="320"> <Image.Source> <BitmapImage DecodePixelWidth="320" UriSource="D:\\xxx.jpg" /> <!--说明:DecodePixelWidth属性告诉图片解码器解码后的位图宽度,对于大图片,不用把图片的所有数据都保存在内存中,可以省内存--> </Image.Source></Image>
var img = new BitmapImage(); img.BeginInit(); img.StreamSource = new MemoryStream(File.ReadAllBytes(path)); img.EndInit(); MyImage.Source = img ;
或
string strfilePathAndName = @"C:\Users\lanmage2\Desktop\faf.jpeg"; BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = new MemoryStream(File.ReadAllBytes(strfilePathAndName)); bitmapImage.EndInit(); bitmapImage.Freeze(); image.Source = bitmapImage; App.Current.Dispatcher.Invoke((Action)delegate () { //PreviewImage = bitmapImage;//非Prism.mvvm架构下,绑定+委托,不能更新UI BGimage.Source = bitmapImage; });
.xmal:
<Image Stretch="Fill" x:Name="imgThumbnail" Canvas.Top="10" Canvas.Right="10" Width="351" Height="286"/>
列表选中,用命令参数传递一个对象imgThumbnail:
.cs:
/// <summary> /// 序列表选中函数 /// </summary> /// <param name="obj"></param> private void SeriesIDTableSelectionChangedFunc(object obj) { try { Image imageVar = obj as Image; if (PreviewSelectIndex >= 0) { string strPath = Environment.CurrentDirectory + "//Image " + "//" ; string strName = "image" + PreviewSelectIndex.ToString() + ".JPG"; string strfilePathAndName = strPath + strName ; BitmapImage bitmapImage= new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = new MemoryStream(File.ReadAllBytes(strfilePathAndName)); bitmapImage.EndInit(); bitmapImage.Freeze(); //PreviewImage.Source = bitmapImage; imageVar.Source = bitmapImage; } } catch (Exception e) { MessageBox.Show("病人历史记录信息表发生【选中】错误,错误信息:--------------" + e.ToString()); } }
关于binding,有几种方法。
https://www.cnblogs.com/seekdream/p/5277237.html
https://bbs.csdn.net/topics/390899995
https://www.cnblogs.com/bolddream/p/4615572.html
https://stackoverflow.com/questions/34616872/wpf-binding-image-source
4.1方法和4.2方法,我没有尝试过,不过,我推荐一个更好的方法,如下所示:
xmal代码:
<Image Stretch="Fill" x:Name="imgThumbnail" Source="{Binding PreviewImage}" Canvas.Top="10" Canvas.Right="10" Width="351" Height="286"/>
.cs代码
属性:
private ImageSource _previewImage; public ImageSource PreviewImage { get { return _previewImage; } set { SetProperty(ref _previewImage, value); } }
图像赋值:
<Image x:Name="imgFaceImage" Stretch="None" Height="322" Width="260" Canvas.Top="16" Canvas.Left="15" Source="{Binding ShowFaceImage}"/>
private Image _showFaceImage; /// <summary> /// 人脸图片 /// </summary> public Image ShowFaceImage { get { return _showFaceImage; } set { SetProperty(ref _showFaceImage, value); } }
Bitmap bmp = new Bitmap(ms);Dispatcher.CurrentDispatcher.Invoke(() => ShowFaceImage = bmp);
关于绑定,我之前采用了下面的错误方法,后来才发现,Image控件的Source本质,就是ImgeSource。所以我们需要绑定 ImageSource,而ImageSource可以直接添加BitmapImage。
因此,ImageSource+BitmapImage一起在Image控件的Source = "{Binding}"绑定中使用。
private BitmapImage PreviewImage { get { return _previewImage; } set { _previewImage = value; OnPropertyChanged("PreviewImage"); } }
PreviewImage = bitmapImage;
private Image _previewImage; /// <summary> /// 预览图片 /// </summary> private Image PreviewImage { get { return _previewImage; } set { SetProperty(ref _previewImage, value); } }
PreviewImage.Source = bitmapImage;
其他的控件绑定,请参考本文。
原网址: 访问
创建于: 2024-06-05 15:52:03
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论