I am having troubles displaying my pop-up from inside a different thread. I have tried reading some of the other questions on here, but I can’t see to understand how I can apply them…
I have an app that uses the camera, and I want to pop up a preview of the picture after its available.
The PhotoCamera has the event ‘CaptureImageAvailable’ and in there I want to pop-up the preview
private void cam_CaptureImageAvailable(object sender, ContentReadyEventArgs e)
{
BitmapImage img = new BitmapImage();
img.SetSource(e.ImageStream);
e.ImageStream.Close();
//creating the new pop-up preview
Grid panelPreview = new Grid();
panelPreview.Background = new SolidColorBrush(Colors.Black);
panelPreview.Width = 300;
panelPreview.Height = 400;
//create an ImageBrush to display the image in the new pop-up
ImageBrush imgPreview = new ImageBrush();
imgPreview.ImageSource = img;
//need a rectangle to add the image to, otherwise can't add it
Rectangle rect = new Rectangle();
rect.Width = 300;
rect.Height = 400;
rect.Fill = imgPreview;
//Adding the rectangle to the grid
panelPreview.Children.Add(rect);
imagePreview.IsOpen = true;
....
}
This causes an UnauthorizedAcessException, which makes sense to me, because this event is on a different thread, but what I don’t understand is how to fix it… I have tried using this code as well
private void cam_CaptureImageAvailable(object sender, ContentReadyEventArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
BitmapImage img = new BitmapImage();
img.SetSource(e.ImageStream);
e.ImageStream.Close();
//creating the new pop-up preview
Grid panelPreview = new Grid();
panelPreview.Background = new SolidColorBrush(Colors.Black);
panelPreview.Width = 300;
panelPreview.Height = 400;
//create an ImageBrush to display the image in the new pop-up
ImageBrush imgPreview = new ImageBrush();
imgPreview.ImageSource = img;
//need a rectangle to add the image to, otherwise can't add it
Rectangle rect = new Rectangle();
rect.Width = 300;
rect.Height = 400;
rect.Fill = imgPreview;
//Adding the rectangle to the grid
panelPreview.Children.Add(rect);
imagePreview.IsOpen = true;
});
This method doesn’t throw any errors, but also doesn’t appear to actually do anything. How do I display this pop-up on the UI thread?
What is “imagePreview”?
If it is anykind of popup or image element container than u must add “panelPreview” as it’s child ,so that it become visible on UI.
One more thing, u can use thumbnail image(via
) for viewing on popup which will take very less memory than actual image.