简体   繁体   中英

Display image from Uri

In a Windows Phone 8 app, I'm using the following code to display an image:

InitializeComponent();
Image i = new Image();
i.Source = new BitmapImage(new Uri("C:\\Data\\Users\\Public\\Pictures\\Sample Pictures\\sample_photo_05.jpg", UriKind.RelativeOrAbsolute));       
LayoutRoot.Children.Add(i);

But when the page loads, the screen is empty. Can anyone see what I'm doing wrong?

Follow below steps to set image by URI in windows phone 1. Copy Image in Images folder in your solution. 2. Set Image as Resource Rightclick on Image->Properties->Build Action ==Content

InitializeComponent();
Image i = new Image();
i.Height =100;
i.Width=100;
i.Source = new BitmapImage(new Uri("/Images/YourImageName", UriKind.RelativeOrAbsolute));       
 LayoutRoot.Children.Add(i);

Copy your image in Asset folder and set Build Action==Content

Image i = new Image();
i.Source = new BitmapImage(new Uri("/yourProjectName;component/Assets/YourImageName", UriKind.RelativeOrAbsolute));       
LayoutRoot.Children.Add(i);

Using a CameraCaptureTask, May be like this

initialize a CameraCaptureTask Object

 CameraCaptureTask cameracapturetask = new CameraCaptureTask();
                    cameracapturetask.Completed += new EventHandler<PhotoResult>(cameracapturetask_Completed);
                    cameracapturetask.Show();

and inside its event

void cameracapturetask_Completed(object sender, PhotoResult e)
        {
            try
            {
                if (e.TaskResult == TaskResult.OK)
                {
                    BitmapImage bmp = new BitmapImage();
                    bmp.SetSource(e.ChosenPhoto);
                    img.Source = bmp;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Copy your image in a folder (Images) and set Build Action==Content

// draw an image, set relative source (in project) and add to LayoutRoot.
var i = new Image{
Source = new BitmapImage(
new Uri("/project;component/Images/image.jpg", UriKind.Relative))
};       
LayoutRoot.Children.Add(i);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM