0

I have an Image control in a WPF Window named "Image1". I am trying to set the picture that is displayed with a png file that I added as a resource.

I am really struggling to figure out how to do it.

I am using VB.Net and I am new to it.

EDIT: I found this while waiting for help:

Image1.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/images/imagenamehere", UriKind.Relative))
djv
  • 15,168
  • 7
  • 48
  • 72
hendersondayton
  • 113
  • 1
  • 2
  • 6

2 Answers2

0

If you have added your images to a folder named images in the root of your project using the Add Existing Item command in Visual Studio, then you can reference your images in a simpler way than you have displayed in your edit. There is no need to use a BitMapImage either. You can set the Image.Source property, either at design time, or at runtime like this:

Image.Source = "/ApplicationName;component/images/ImageName.png";

Although, I'd like to add that in WPF, the best way to set an Image.Source at runtime is with a Binding. In this respect, you'd change the string property that was data bound to the Image.Source property to the above string to change the image.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
0

I tried to set the icon property of a window like mentioned above but it returned an error. I search and found a solution. I don't know if it is the best solution but maybe someone has got the same problem.

Solution:

windowX.Icon = BitmapFrame.Create(Application.GetResourceStream(New Uri("Resources\addX.png", UriKind.RelativeOrAbsolute)).Stream)
Crow1973
  • 31
  • 5