Click here to Skip to main content
15,884,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Referring to https://msdn.microsoft.com/en-us/library/system.windows.controls.image.source(v=vs.110).aspx[^] I wrote a piece of code below in my SL project. My purpose is to create an Image control. This piece of code is in my MainPage.xaml.cs. The images are stored in \Images\, which is parallel to the MainPage.xaml.cs under the project tree. Unfortunately, the images do not display. What's wrong in my code (I guess it is uri specified here). Thanks.
The related code is below:
C#
spNewItem.Children.Add(BuildImageButton(@"Images\fan1.ico", h, w));
...
        public System.Windows.Controls.Image BuildImageButton(string source, int h, int w)  {
            System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage();
            bi.UriSource = new Uri(source, UriKind.Relative);

            Image img = new Image { Width = w, Height = h, Source = bi};
            img.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            img.Margin = new Thickness(5, 0, 0, 0);
            return img;
        }
Posted
Comments
s yu 27-May-15 13:45pm    
Thru. research around and multiple tries, the prob. got solved. The solution is below:
spNewItem.Children.Add(BuildImageButton("/projectName;component/Images/myImage.png", h, w));

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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