如下:主要使用pack这种形式
public BitmapImage GetBitmapImage(string argName, string argCatalog = null)
{
try
{
string path = string.Empty;
if (string.IsNullOrEmpty(argCatalog))
{
path = @"pack://SiteOfOrigin:,,,/" + argName;
}
else
{
path = @"pack://SiteOfOrigin:,,,/" + argCatalog + "/" + argName;
}
var bi = new BitmapImage();
bi.BeginInit();
// 创建 BitmapImage 后关闭流,请将 CacheOption 属性设置为 BitmapCacheOption.OnLoad。
// 默认 OnDemand 缓存选项保留对流的访问,直至需要位图并且垃圾回收器执行清理为止。
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.UriSource = new Uri(path);
bi.EndInit();
bi.Freeze();
return bi;
}
catch (Exception ex)
{
return null;
}
}