Load Images in WPF application
Posted
by LLS
on Stack Overflow
See other posts from Stack Overflow
or by LLS
Published on 2010-06-16T16:09:46Z
Indexed on
2010/06/16
16:12 UTC
Read the original article
Hit count: 386
I am not familiar with WPF, and I just feel quite confusing.
I am trying to create a little computer game, and there are elements I want to display dynamically. I use Image class and add the images to a canvas. But I'm not sure whether it's a good practice. I feel that adding controls to canvas seem to be a little wired. And I'm a little concerned about the performance, because I may need many images.
The real problem is, I can't load the images from the image files. I see an example in a text book like this (XMAL):
<Image Panel.ZIndex="0" Margin="0,0,0,0" Name ="image1">
<Image.Source>
<BitmapImage UriSource="Bell.gif" />
</Image.Source>
</Image>
Where Bell.gif is added into the project.
And I tried to copy this in code to create an image.
new Image { Source = new BitmapImage(new Uri("Blockade.bmp"))}
But I got invalid Uri exception. After some search in the Internet, I found that loading resources dynamically seems to be difficult. Can I load the files added to the project dynamically?
If I use absolute path then it's OK. But I can't expect every computer will put the files in the same location. Can I use relative path?
I tried
new Image { Source = new BitmapImage(new Uri(@"Pictures\Blank Land.bmp", UriKind.Relative)) }
But it doesn't work. (The image is blank)
Thanks.
© Stack Overflow or respective owner