5

I am creating stackpanel inside the gridview cell dynamically and placing a image control inside stackpanel but on the window only blank space coming, not the image
here is what i am doing

StackPanel Stkpnl = new StackPanel();

Image BgImg = new Image();
BitmapImage Img = new BitmapImage(new Uri(
    "Images/cellbg.png", UriKind.Relative));
BgImg.Source = Img ;

Stkpnl.Children.Add(BgImg );
2
  • 1
    Have you tried debugging to see if the Img is loaded - have you added the stackpanel to the visual tree? Commented Aug 10, 2012 at 17:09
  • 1
    Possible duplicate of Setting WPF image source in code Commented Feb 18, 2016 at 5:58

3 Answers 3

2

You need to use a Pack URI. See https://stackoverflow.com/a/1651397/486660

If you have a question as to what your Pack URI should look like, then temporarily add an Image control to your WPF page then goto the Source property and click the "..." button and navigate to your image. The property's text box will have the Pack URI in it.

Sign up to request clarification or add additional context in comments.

Comments

0

Your code seems to be correct , have you check that image is existing on the folder or the relative path is correct, if you have added this image later to the image folder than you have to set "Build Action" of a image to "Resource". right click on the image in solution explorer than you can set this property.

you should also check this question

Setting WPF image source in code

Comments

0
StackPanel Stkpnl = new StackPanel();
Image BgImg = new Image();    
String packuri = "pack://application:,,,/yourassemblyname;component/cellbg.png";
BgImg.Source = new ImageSourceConverter().ConvertFromString(packuri) as ImageSource;

Stkpnl.Children.Add(BgImg );

PS: To find the assembly name in visual studio 2012 go to "Project" in menu bar -> "setup properties"->Application.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.