开发者

Creating Images from an Embedded Resource

This has been driving me crazy for the past 2 days, and anything close that I find just does not seem to work in my situation, perhaps someone can point out what I'm doing wrong.

I have a WPF project to which I've include a fairly large number of images(80ish). I've added them to the project into a folder called "Images". They are currently set to be embedded resources(though I've tried regular resources as well) but nothing seems to work.

I am attempting to create and load an ArrayList of type Image using these files as the source, but I'm failing pretty bad.

private void addPicture(string name)
{            
    string filePath = "pack://MyApp_WPF:,,,/Images/" + name + ".png";
    Stream imageStream = assembly.GetManifestResourceStream(filePath);

    Image curImage = new Image();
    BitmapImage bmpImage = new BitmapImage();
    bmpImage.BeginInit();
    bmpImage.UriSource = new Uri(filePath, UriKind.Relative);
    bmpImage.EndInit();
    curImage.Height = 60;
    curImage.Stretch = Stretch.Uniform;
    curImage.Source = bmpImage;
    curImage.Margin = new Thickness(5);

    imageList.Add(curImage);
}

It was much easier to do in my windows form application, but I just cant figure it out with WPF... Any help besides links to r开发者_JAVA技巧esources would be nice, because chances are at this point I've already read them.


First thing to note is that there is an DownloadError event that you ought to subscribe to, this might explain what is happening and why (though sometimes the error message is rather generic); the next is that you might not need the pack:// URI construct with images build actions of Resource:

var bmpImage = new BitmapImage(new Uri(
    string.Format("/Images/{0}.png", name", UriKind.Relative));


The Pack URI is wrong, where you name your assembly you should place an authority, in this case application, i.e.

string filePath = "pack://application:,,,/Images/" + name + ".png";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜