下载图像并用作资源

时间:2013-07-07 16:14:54

标签: c# image downloadfile

在WPF C#中,我在运行时使用.DownloadFile()方法从网上下载图像,并将它们保存在名为“。\ Images \ DownloadedPosters \”的特定文件夹中,但是当我尝试在编程生成的UI中使用它们时使用图像路径作为URL的元素根本不显示图像。

我认为这是因为图像未使用BuildAction类型设置为资源。

我该如何对抗这个?我是否必须将下载的图像嵌入资源文件中?

1 个答案:

答案 0 :(得分:1)

在这里:Same question。 使用此加载例程:

var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.UriSource = new Uri("img.jpg", UriKind.Relative);
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
img.Source = bitmapImage;