Windows Phone 7 Silverlight: How to Load an Image using C#

by sunil ravulapalli /18. March 2010 03:39 /windows-phone /Comments (0)
public MainPage()

           SupportedOrientations = SupportedPageOrientation.Portrait |

           Image img = new Image();

           //Size the Image
           img.Height = 80;
           img.Width = 80;

           //Place it top left
           img.HorizontalAlignment = HorizontalAlignment.Left;
           img.VerticalAlignment = VerticalAlignment.Top;

           //Point at the physical file
           Uri uri = new Uri("1.JPG", UriKind.Relative);
           ImageSource imgsource = new System.Windows.Media.Imaging.BitmapImage(uri);
           //Set image property
           img.SetValue(Image.SourceProperty, imgsource);
           //Add it to the main grid


Find the MainPage() function in the MainPage.xaml.cs and paste the code. Make sure there is image file in the project folder. When the windows emulator loads the image stretches to the entire screen. That is why you need to specify the Height and Width. We also need to specify the Horizontal and Vertical alignments, otherwise the image will appear in the center of the screen.

blog comments powered by Disqus