beny lim
beny lim

Reputation: 1304

Passing image from one page to another windows phone 7

I am trying to pass an image in a image control of one page to another page. For example, i have a image control named "image1" in Page1.xaml and i want the image shown in "image1" to be pass over/ shown in Page2.xaml ink canvas. Can you help me with the coding of doing it. Thank.

Upvotes: 0

Views: 1354

Answers (1)

Ku6opr
Ku6opr

Reputation: 8126

First page saves image to Isolated Storage as a file 'photo.jpg'. 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();
    }
}

Page2:

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();
        }
    }
}

Upvotes: 1

Related Questions