查资料后决定修改为流的方式加载图片:如下,
System.IO.MemoryStream ms = new System.IO.MemoryStream();
Image myImage3 = new Image();
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.StreamSource = ms; //将流对象给StreamSource
bi3.EndInit();
myImage3.Stretch = Stretch.Fill;
myImage3.Source = bi3;
public static BitmapImage GetImage(string imagePath)
{
BitmapImage bitmap = new BitmapImage();
if (File.Exists(imagePath))
{
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
using (Stream ms = new MemoryStream(File.ReadAllBytes(imagePath)))
{
bitmap.StreamSource = ms;
bitmap.EndInit();
bitmap.Freeze();
}
}
return bitmap;
两段代码摘取要用的合并,OK。