0

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.

beny lim
  • 1,312
  • 3
  • 28
  • 57

1 Answers1

1

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();
        }
    }
}
Ku6opr
  • 8,126
  • 2
  • 24
  • 31
  • By the way why using photo chooser task?? – beny lim Jun 29 '11 at 12:41
  • It's just sample to generate image. You must save your image from your source – Ku6opr Jun 29 '11 at 16:13
  • If you have already loaded image into your control, you can save it with next code: 'WriteableBitmap ImageToSave = new WriteableBitmap(PageImage, null)', where PageImage - your UIElement. Then use code above to save and load to Isolated Storage – Ku6opr Jun 29 '11 at 16:46