Transferring an image from one page to another Windows Phone 7

I am trying to pass an image in an image control from one page to another page. For example, I have an image control named "image1" in Page1.xaml and I want the image shown in "image1" to pass or show in Page2.xaml the ink canvas . Can you help me with coding this. Thank.

0


source to share


1 answer


The first page saves the image to isolated storage as a 'photo.jpg' file. The second page read it.

Page1:

public partial class Page1 : PhoneApplicationPage
{
    PhotoChooserTask PhotoChooser;
    int ImageWidth, ImageHeight;
    // Constructor
    public Page1()
    {
        InitializeComponent();

        ImageWidth = (int)PageImage.Width;
        ImageHeight = (int)PageImage.Height;

        PhotoChooser = new PhotoChooserTask();
        PhotoChooser.PixelWidth = ImageWidth;
        PhotoChooser.PixelHeight = ImageHeight;
        PhotoChooser.ShowCamera = true;

        PhotoChooser.Completed += new EventHandler<PhotoResult>(PhotoChooser_Completed);
    }

    void PhotoChooser_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            IsolatedStorageFile Store = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream Stream = new IsolatedStorageFileStream("photo.jpg", FileMode.Create, FileAccess.Write, Store);

            WriteableBitmap myImage = new WriteableBitmap(ImageWidth, ImageHeight);
            myImage.SetSource(e.ChosenPhoto);
            PageImage.Source = myImage;
            PageImage.InvalidateArrange();
            myImage.SaveJpeg(Stream, ImageWidth, ImageHeight, 0, 100);

            Stream.Close();
        }
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        if (IsolatedStorageFile.GetUserStoreForApplication().FileExists("photo.jpg"))
        {
            IsolatedStorageFile Store = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream Stream = new IsolatedStorageFileStream("photo.jpg", FileMode.Open, FileAccess.Read, Store);

            WriteableBitmap myImage = new WriteableBitmap(ImageWidth, ImageHeight);
            myImage.LoadJpeg(Stream);

            PageImage.Source = myImage;
            PageImage.InvalidateArrange();
            Stream.Close();
        }
    }

    private void ToPage2Button_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative));
    }

    private void LoadButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        PhotoChooser.Show();
    }
}

      



Page 2:

public partial class Page2 : PhoneApplicationPage
{
    int ImageWidth, ImageHeight;

    public Page2()
    {
        InitializeComponent();

        ImageWidth = (int)PageImage.Width;
        ImageHeight = (int)PageImage.Height;
    }

    private void BackButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        NavigationService.GoBack();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        if (IsolatedStorageFile.GetUserStoreForApplication().FileExists("photo.jpg"))
        {
            IsolatedStorageFile Store = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream Stream = new IsolatedStorageFileStream("photo.jpg", FileMode.Open, FileAccess.Read, Store);

            WriteableBitmap myImage = new WriteableBitmap(ImageWidth, ImageHeight);
            myImage.LoadJpeg(Stream);

            PageImage.Source = myImage;
            PageImage.InvalidateArrange();
            Stream.Close();
        }
    }
}

      

+1


source







All Articles