ivan-crg
ivan-crg

Reputation: 43

Windows phone picturebox

I'm developing an windows phone 8.1 application in C#. I'm using the camera to take a picture. The picture is than saved on the device and I'm trying to show it in a picturebox. I have tested it on a HTC phone and it worked nice, but when i tried it on a Nokia Lumia the picture would never load. Does anyone have an idea how to solve that?

Here is the code I am sing to take a picture:

    private void snap_task_Click(object sender, EventArgs e)
    {
        cameraCaptureTask = new CameraCaptureTask();
        cameraCaptureTask.Completed += cameraCaptureTask_Completed;
        cameraCaptureTask.Show();

    }

    void cameraCaptureTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            NavigationService.Navigate(new Uri("/Slika.xaml?fotka=" + e.OriginalFileName, UriKind.Relative));

        }
    }

And this is the code where I try toload the picture.

    public Slika()
    {
        InitializeComponent();
        string slika = string.Empty;
        string slika2 = string.Empty;
        this.Loaded += (s, e) =>
        {

            if (NavigationContext.QueryString.TryGetValue("fotka", out slika))
            {

                putanja = slika; /*"/Resources/" + slika + ".png";/**/

                int x = putanja.Length;

                if (x == 1)
                {
                    putanja = "/Resources/" + putanja + ".png";
                    uriPutanja = new Uri(putanja, UriKind.Relative);
                    fotka = new BitmapImage(uriPutanja);
                }
                else
                {
                    uriPutanja = new Uri(putanja, UriKind.Relative);
                    porukaTextBox.Text = putanja;
                    fotka = new BitmapImage(uriPutanja);
                }
            }
            img1.Source = fotka;

        };

    }

PS

the loading from local resources works fine on both phones, it is just the "else" part of the if that is causing problems on the Nokia.

Upvotes: 4

Views: 503

Answers (3)

user2700896
user2700896

Reputation: 579

You are saving the image in the Camera roll folder in your phone, try saving it on your memory card instead and try if that works (you an just change it in the setting of the phone and say to save the new pictures on the SD card) If that works, try using the PhotoChooserTask in order to get the image. I hope that the following code will help you:

    using Microsoft.Phone.Tasks;
    using System.IO;
    using System.Windows.Media.Imaging;
    ...
    PhotoChooserTask selectphoto = null;
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        selectphoto = new PhotoChooserTask();
        selectphoto.Completed += new EventHandler(selectphoto_Completed);
        selectphoto.Show();
    }
    void selectphoto_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            BinaryReader reader = new BinaryReader(e.ChosenPhoto);
            image1.Source = new BitmapImage(new Uri(e.OriginalFileName));
        }
    }

Upvotes: 3

JTIM
JTIM

Reputation: 2771

As I understand the code you have forgotten the .png in the else case.

Upvotes: 0

user2700896
user2700896

Reputation: 579

You can try changing the UriKind from Relative to Absolute. If I have understood your code, you will get the absolute path to the picture.

Upvotes: 2

Related Questions