Ajay
Ajay

Reputation: 6590

Load image on canvas Windows phone

I am choosing image using PhotoChooserTask. I am trying to load selected image on canvas but not able to load.

Here is my code

 void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            Image image = new Image();
            string path = e.OriginalFileName;
            Uri uri = new Uri(path, UriKind.Relative);

            ImageSource img = new System.Windows.Media.Imaging.BitmapImage(uri);

            image.Height = paint.Height;
            image.Width = paint.Width;

            image.SetValue(Image.SourceProperty, img);

            Canvas.SetLeft(image, 50);
            Canvas.SetTop(image, 50);

            paint.Children.Add(image);
        }
    }

MainPage.xaml

 <Canvas x:Name="paint" Background="Transparent" Margin="0,95,0,139" >

 </Canvas>

I am getting why this is not working. Is there any change in my code?

Upvotes: 1

Views: 1644

Answers (1)

anothershrubery
anothershrubery

Reputation: 21023

Added my comment as an answer as I'm pretty sure this is the problem.

Should you be setting the position of the image in the canvas via the Canvas.Left and Canvas.Top attributes? Eg.

Canvas.SetLeft(image, 10); 
Canvas.SetTop(image, 10); 
paint.Children.Add(image);

Upvotes: 1

Related Questions