加载方式01:
在 Xaml 中设置图片源
<UserControl x:Class="Demo.Views.ImageView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Demo.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<!--以此方式加载图片后,会锁定文件-->
<Image Source="\Pictures\bg7r-fyrpeie1413753.jpg"></Image>
</UserControl>
加载方式02:
以代码的方式设置图片源
private ImageSource GetImageSource(Uri uri)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
// 此缓存方式会锁定文件
bitmap.CacheOption = BitmapCacheOption.Default;
bitmap.UriSource = uri;
bitmap.EndInit();
bitmap.Freeze();
return bitmap;
}
// Source 绑定到 Image.Source
Source = GetImageSource(new Uri(path));
加载方式03:
以代码的方式设置图片源
private ImageSource GetImageSource(Uri uri)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
// 此缓存方式,不会锁定文件
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.UriSource = uri;
bitmap.EndInit();
bitmap.Freeze();
return bitmap;
}
// Source 绑定到 Image.Source
Source = GetImageSource(new Uri(path));
加载方式04:
以代码的方式设置图片源
private ImageSource GetImageSource(Stream stream)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
// 此缓存方式,不会锁定文件
bitmap.CacheOption = BitmapCacheOption.OnLoad;
using (stream)
{
bitmap.StreamSource = stream;
bitmap.EndInit();
bitmap.Freeze();
}
return bitmap;
}
// Source 绑定到 Image.Source
var file = File.Open(path, FileMode.Open);
Source = GetImageSource(file);