Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have .PNG files in the debug folder that I use as imagebrushes and diffusematerials with no issue. I.E.:
C#
myImageBrush.ImageSource = New BitmapImage(New Uri("image0.png", UriKind.Relative))

Now I have two Image controls and I set the source I.E.:
C#
myImage.Source = New BitmapImage(New Uri("image0.png", UriKind.Relative))

Nothing appears.

What I have tried:

Unless I set a breakpoint, inspect the Image in debug AND expand the source section and scroll to the bottom and then continue. If I do that, the image displays - I don't have to do anything else.

SO I have .PNG files that work fine - except on Image controls. If I expand the Image details in debug mode, I have no issue, otherwise the Image simply doesn't display.

Any idea what is going on?
Posted
Updated 8-Sep-23 9:52am
v2
Comments
Richard MacCutchan 8-Sep-23 5:33am    
Where is the code that actually paints the image?
kamikazehighland 8-Sep-23 8:09am    
Private myImage As New Image

Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
myImage.Margin = New Thickness(12, 12, 0, 0)
myImage.HorizontalAlignment = HorizontalAlignment.Left
myImage.Width = 100
myImage.Source = New BitmapImage(New Uri("image0.png", UriKind.Relative))
myGrid.Children.Add(myImage)
End Sub
Richard MacCutchan 8-Sep-23 10:20am    
Take a look at How to: Use the Image Element - WPF .NET Framework | Microsoft Learn[^], it shows how you should set imager properties.
[no name] 8-Sep-23 11:16am    
Add an "ImageFailed" event handler. Since you're doing "relative" addressing, it probable couldn't find the image "relative" to the image control.

https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.image.imagefailed?view=windowsdesktop-7.0
kamikazehighland 8-Sep-23 12:54pm    
It's odd that "New BitmapImage(New Uri("image0.png", UriKind.Relative))" works well for Imagebrushes but not Images. I put the full path with UriKind.Absolute and it works with no issue. And especially odd that when I pause and view the Source info that makes it work all of a sudden.

1 solution

I'm so late, but
how about my suggestion?
var bitmapImage = new BitmapImage();

bitmapImage.BeginInit();

bitmapImage.UriSource = new Uri("pen.png", UriKind.RelativeOrAbsolute);

bitmapImage.CacheOption = BitmapCacheOption.OnLoad;

bitmapImage.EndInit();

MyImage.Source = bitmapImage;



from WPF Relative Image Path Problem (BitmapImage and UriSource) ~ Ladofa[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900