博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
解决WPF两个图片控件显示相同图片因线程占用,其中一个显示不全的问题
阅读量:4609 次
发布时间:2019-06-09

本文共 1062 字,大约阅读时间需要 3 分钟。

在做项目的过程中遇到这样一个问题,下面提出一种解决方法,主要思想是图片的Copy,如还有其他方法,欢迎交流。
在前端图片控件绑定显示时,使用转换器进行转义绑定
 
(1)转换器:
public class ImgConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            using (BinaryReader loader = new BinaryReader(File.Open(value.ToString(), FileMode.Open)))
            {
                FileInfo fd = new FileInfo(value.ToString());
                int Length = (int) fd.Length;
                byte[] buf = new byte[Length];
                buf = loader.ReadBytes((int) fd.Length);
                loader.Dispose();
                loader.Close(); //开始加载图像
                BitmapImage bim = new BitmapImage();
                bim.BeginInit();
                bim.StreamSource = new MemoryStream(buf);
                bim.EndInit();
                GC.Collect();
                return bim;
            }
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return parameter;
        }
    }
 
(2)在页面加载转换器资源
  <converters:ImgConverter x:Key="ImgCnverter"/>
(3)图片控件绑定转换器
xaml代码:
<Image Width="120" Height="80" Source="{Binding ImageLocalPath,Converter={StaticResource ImgCnverter}}"/>
 
显示效果如下图:

 

转载于:https://www.cnblogs.com/skdsxx/p/10045148.html

你可能感兴趣的文章
3、流程语句相关练习
查看>>
30、git 使用
查看>>
iOS网络-02-数据解析(JSON与XML)
查看>>
python列表求和的几种等效电路
查看>>
Luogu P3393 逃离僵尸岛
查看>>
Flatten Binary Tree to Linked List
查看>>
Edit Distance
查看>>
软件工程第一次作业补充
查看>>
N76E003---输入捕获
查看>>
poj 1094 Sorting It All Out(拓扑排序)
查看>>
acdream B - 郭式树 (水题 卡cin,cout, 卡LL)
查看>>
BMP图像格式
查看>>
python的匿名函数lambda解释及用法
查看>>
c#遍历Dictionary使用KeyValuePair
查看>>
defineProperties属性的运用==数据绑定
查看>>
关于 IOS 发布的点点滴滴记录(一)
查看>>
《EMCAScript6入门》读书笔记——14.Promise对象
查看>>
CSS——水平/垂直居中
查看>>
Eclipse连接mysql数据库jdbc下载(图文)
查看>>
Python中Selenium的使用方法
查看>>