如何强制图像控件关闭在WPF中打开的文件 [英] How to force Image control to close the file that it opens in wpf
问题描述
我的wpf页面上有一个图像,该图像从硬盘上打开图像文件。用于定义图像的XAML是:
< Image Canvas.Left = 65 Canvas.Top = 5 Width = 510高度= 255来源= {Binding Path = ImageFileName} />
我正在使用Caliburn Micro,并且ImageFileName已更新为图像控件应显示的文件名。 / p>
通过图像控件打开图像时,我需要更改文件。但是该文件已被图像控件锁定,我无法删除或复制该文件上的任何图像。打开文件后或需要在其上复制另一个文件时,如何强制Image关闭文件?
我检查了,没有CashOptio用于图像,所以我不能使用它。
您可以使用如下所示的绑定转换器,它通过设置 BitmapCacheOption.OnLoad 。该文件将立即加载,之后不会被锁定。
< Image Source = {Binding。 ..,
Converter = {StaticResource local:StringToImageConverter}} />
转换器:
公共类StringToImageConverter:IValueConverter
{
公共对象Convert(
对象值,类型targetType,对象参数,CultureInfo文化)
{
对象结果= null;
var path =字符串形式的值;
if(!string.IsNullOrEmpty(path))
{
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource =新的Uri(路径);
image.EndInit();
结果=图片;
}
返回结果;
}
公共对象ConvertBack(对象值,类型targetType,对象参数,CultureInfo文化)
{
throw new NotSupportedException();
}
}
更好的是,直接从FileStream加载BitmapImage:
公共对象Convert(
对象值,类型targetType,对象参数,CultureInfo文化)
{
对象的结果= null;
var path =字符串形式的值;
if(!string.IsNullOrEmpty(path)&& File.Exists(path))
{
使用(var stream = File.OpenRead(path))
{
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource =流;
image.EndInit();
结果=图片;
}
}
返回结果;
}
I have an image on my wpf page which opens an image file form hard disk. The XAML for defining the image is:
<Image Canvas.Left="65" Canvas.Top="5" Width="510" Height="255" Source="{Binding Path=ImageFileName}" />
I am using Caliburn Micro and ImageFileName is updated with the name of file that image control should show.
When the image is opend by image control, I need to change the file. But the file is locked by image control and I can not delete or copy any mage over it. How can I force Image to close the file after it opened it or when I need to copy another file over it?
I checked and there is no CashOptio for image so I can not use it.
You could use a binding converter like below that loads an image directly to memory cache by setting BitmapCacheOption.OnLoad. The file is loaded immediately and not locked afterwards.
<Image Source="{Binding ...,
Converter={StaticResource local:StringToImageConverter}}"/>
The converter:
public class StringToImageConverter : IValueConverter
{
public object Convert(
object value, Type targetType, object parameter, CultureInfo culture)
{
object result = null;
var path = value as string;
if (!string.IsNullOrEmpty(path))
{
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(path);
image.EndInit();
result = image;
}
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
Even better, load the BitmapImage directly from a FileStream:
public object Convert(
object value, Type targetType, object parameter, CultureInfo culture)
{
object result = null;
var path = value as string;
if (!string.IsNullOrEmpty(path) && File.Exists(path))
{
using (var stream = File.OpenRead(path))
{
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = stream;
image.EndInit();
result = image;
}
}
return result;
}
这篇关于如何强制图像控件关闭在WPF中打开的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!