繁体   English   中英

无效的跨线程访问

[英]Invalid cross-thread access

当我尝试在我的cam_CaptureImageAvailable方法中使用BitMapImage时,我得到一个无效的跨线程访问,我尝试使用Dispatcher但是这给了我一个无法访问已关闭的Stream错误,System.ObjectDisposedException未处理。

// Informs when full resolution picture has been taken, saves to local media library and isolated storage.
    void cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e)
    {

        string fileName = folderName+"\\MyImage" + savedCounter + ".jpg";

        try
        {   // Write message to the UI thread.
            Deployment.Current.Dispatcher.BeginInvoke(delegate()
            {
                txtDebug.Text = "Captured image available, saving picture ," + fileName;
            });

            // Save picture to the library camera roll.
            //library.SavePictureToCameraRoll(fileName, e.ImageStream);


            // Write message to the UI thread.
            Deployment.Current.Dispatcher.BeginInvoke(delegate()
            {
                //txtDebug.Text = "Picture has been saved.";

            });

            // Set the position of the stream back to start
            e.ImageStream.Seek(0, SeekOrigin.Begin);


            // Save picture as JPEG to isolated storage.
            using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
                {

                    // Initialize the buffer for 4KB disk pages.
                    byte[] readBuffer = new byte[4096];
                    int bytesRead = -1;

                    // Copy the image to isolated storage. 
                    while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
                    {
                        targetStream.Write(readBuffer, 0, bytesRead);

                    }

                }

                var bitmapImage = new BitmapImage(); 
                bitmapImage.SetSource(e.ImageStream); 
                WriteableBitmap wb = new WriteableBitmap(bitmapImage);
                IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication(); 
                using (IsolatedStorageFileStream myFileStream = myStore.CreateFile(fileName)) 
                { 
                    wb.SaveJpeg(myFileStream, 10, 10, 0, 70); 
                }

            }





        }
        finally
        {
            // Close image stream
            e.ImageStream.Close();
        }

    }

试过这个

Deployment.Current.Dispatcher.BeginInvoke(delegate()
                {
                    var bitmapImage = new BitmapImage();
                    bitmapImage.SetSource(e.ImageStream);
                    WriteableBitmap wb = new WriteableBitmap(bitmapImage);
                    IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
                    using (IsolatedStorageFileStream myFileStream = myStore.CreateFile(fileName))
                    {
                        wb.SaveJpeg(myFileStream, 10, 10, 0, 70);//compressing image
                    }
                });

任何人都可以帮助我,我真的很感激,谢谢。

您必须在UI线程上创建BitmapImageWriteableBitmap ,因为它们是DependencyObject

您收到ObjectDisposedException错误的原因是,当调度程序处理您的请求时,图像的流已经关闭。

您是否尝试将该代码移动到Dispatcher调用中?

 e.ImageStream.Close();

调度程序的代码不会立即执行,而是在稍后的某个时间点执行,但您已经关闭了该流。

如果这不起作用,那么您可以通过在CaptureImageAvailable读取内存流来解决您的问题,然后将该MemoryStream作为源传递给您的BitmapImage

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM