【发布时间】:2017-10-11 18:32:21
【问题描述】:
我正在尝试从 WPF 窗口中的文件加载位图图像,但在下面的 EndInit() 行中出现异常
BitmapImage img = new BitmapImage();
img.BeginInit();
img.CacheOption = BitmapCacheOption.OnLoad;
img.UriSource = PackUriHelper.Create(new Uri(@"pack://application:,,,/Images/TestImage.png", UriKind.Absolute));
img.EndInit();
img.Freeze();
有什么想法吗?如果它完全相关,则此 WPF 窗口托管在本机应用程序中。
【问题讨论】:
-
请将 StackTrace 作为文本发布,不要作为图片发布
-
PackUriHelper.Create在这里似乎没有意义,因为您已经有了 Pack URI。img.UriSource = new Uri(@"pack://application:,,,/Images/TestImage.png")在标准 WPF 应用程序中就足够了。对于非 WPF 应用程序,this answer 可能会有所帮助。 -
MSDN - 如果我不使用 PackUriHelper,我会得到一个不同的异常“无法识别 URI 前缀”