user3008134
user3008134

Reputation: 127

Passing image between two pages

I just pass the image between two pages, by passing the byte[] and I try to convert the byte[] to image in page 2 using the following code,

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    if (e.Uri.OriginalString.Contains("img"))
    {
        expenseDetails = AuthOrgClass.expenseDetails;
        imgData = NavigationService.GetImageNavigationData();
        fill();   
    }
}

private void fill()
{
   var bitmapImage = new BitmapImage();
   var memoryStream = new MemoryStream(imgData);
   bitmapImage.SetSource(memoryStream);
   ImageBox.Source = bitmapImage;
}

While executing the line bitmapImage.SetSource(memoryStream);
I get the exception

An exception of type 'System.Exception' occurred in System.Windows.ni.dll but was not handled in user code

What could be the problem?

Upvotes: 0

Views: 123

Answers (2)

Jaihind
Jaihind

Reputation: 2778

You should use IsolatedStorageSettings to store image as byte array. IsolatedStorageSettings accessible in all application. So you can easily pass your byte array between any pages in application. try this may this will help you.

 SaveImageAsByteArray()
    {
    IsolatedStorageSettings MemorySettings = IsolatedStorageSettings.ApplicationSettings;
    if (MemorySettings.Contains("ImageData"))
     MemorySettings["ImageData"] = your byte array;
    else
    MemorySettings.add("ImageData", your byte array;);

    IsolatedStorageSettings.ApplicationSettings.Save();
    }



    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        if (e.Uri.OriginalString.Contains("img"))
        {

            fill();   
        }
    }

    private void fill()
    {
    IsolatedStorageSettings MemorySettings = IsolatedStorageSettings.ApplicationSettings;
     if (MemorySettings.Contains("ImageData"))
     byte[] bytes = MemorySettings["ImageData"] 
     MemoryStream stream = new MemoryStream(bytes);
     BitmapImage image = new BitmapImage();
     image.SetSource(stream);
       ImageBox.Source = image;
    }

Upvotes: 1

Rahul
Rahul

Reputation: 101

The best Practice is take a global Image varible in App.xaml.cs,assign it at first page and take value from it at second page.It would never create a problem..:)

Upvotes: 0

Related Questions