TS-
TS-

Reputation: 4411

Adding image F#

I have a stack panel to which I want to add some icons dynamically.

If I add a TextBlock to the stack panel, it works perfectly:

// assuming stackPanel is my stack panel
let text = new TextBlock()
text.Text <- "Test"
stackPanel.Children.add(text)

However, my goal is to add an image, but seems like it fails to resolve the image

let getImageSource(imagePath) = 
    let uri = new Uri(imagePath, UriKind.Relative)
    new BitmapImage(uri); 

let icon = new Image()
icon.Source <- getImageSource("images/fileIcon/icon.gif")

stackPanel.Children.Add(icon) // this doesnt work

now when I do:

let icon = new Image()
icon.Source <- getImageSource("images/fileIcon/icon.gif")

stackPanel.Children.Add(icon) 

let text = new TextBlock()
text.Text <- "Test"
stackPanel.Children.add(text)

I can see there's an empty space between the texts, as if there is an empty image there. So my guess is there's something wrong with the way I resolve the image path - but I am not sure why.

Thoughts?

Thanks!

Upvotes: 3

Views: 498

Answers (2)

Stefan Dragnev
Stefan Dragnev

Reputation: 14473

In case your gif's Build Action is Resource, then the proper way to address it is /SilverlightApplication1;component/path/to/file.gif. Here SilverlightApplication1 is the name of your silverlight application

In case it's Build Action is Content, then it's proper address is /path/to/file.gif, always with a leading slash when creating a BitmapImage.

Check out Silverlight 2: Demystifying URI references for app resources for more information.

For easier debugging of image loading problems, hook to the BitmapImage.ImageFailed event and see what kind of errors crop up.

One last note, AFAIK Silverlight doesn't support the GIF format. You might use PNG instead.

Upvotes: 5

Nyi Nyi
Nyi Nyi

Reputation: 788

You can try with the following Uri if yours is WPF App.

let uri = Uri("pack://application:,,,/asm_name;component/images/fileIcon/icon.gif")

asm_name have to be replaced with your actual assembly name.

if your are working on Silverlight application, you need to modify the uri like this. Assuming that the build action of icon.gif is Resource.

let uri = Uri("../images/fileIcon/icon.gif", UriKind.Relative)

Hope this helps.

Upvotes: 0

Related Questions