2

I am trying to retrieve an image from the phone library and set it as the page background using the following code

    private void selectImageFromMediaLib()
    {
         selectphoto = new PhotoChooserTask();
         selectphoto.Completed += new EventHandler<PhotoResult>(selectphoto_Completed);
     selectphoto.Show();
    }

    private void selectphoto_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            backgroundUri = new Uri(e.OriginalFileName, UriKind.Absolute);
            var bitmap = new BitmapImage(backgroundUri);
            ImageBrush imageBrush = new ImageBrush();
            imageBrush.ImageSource = bitmap;
            this.LayoutRoot.Background = imageBrush;     
     }
    }

However, the page background turns black so the photo was not retrieved/created correctly. What is the correct path for the URI to the device library? Isn't using UriKind.Absolute enough?

enter image description here

4

2 回答 2

0

您不能使用PhotoResult.OriginalFileName属性来读取文件,而是使用 PhotoResult.ChosenPhoto流并将其分配给bitmap.ImageSource代码中的属性。

于 2013-04-07T11:02:02.123 回答
0

尝试这个。这个对我有用

PhotoChooserTask selectphoto;
private void selectImageFromMediaLib()
{
   selectphoto = new PhotoChooserTask();
   selectphoto.Completed += new EventHandler<PhotoResult>(selectphoto_Completed);
   selectphoto.Show();
}

private void selectphoto_Completed(object sender, PhotoResult e)
{
   if (e.TaskResult == TaskResult.OK)
   {
       var imageBytes = new byte[e.ChosenPhoto.Length];
       e.ChosenPhoto.Read(imageBytes, 0, imageBytes.Length);
       BitmapImage bitmapImage = new BitmapImage();
       MemoryStream ms = new MemoryStream(imageBytes);
       bitmapImage.SetSource(ms);
       ImageBrush imageBrush = new ImageBrush();
       imageBrush.ImageSource = bitmapImage;
       this.LayoutRoot.Background = imageBrush;
    }
}
于 2016-04-24T07:05:01.650 回答