C# 无效的跨线程访问
当我尝试在cam_CaptureImageAvailable方法中使用BitMapImage时,我获得了无效的跨线程访问。我尝试使用Dispatcher,但这给了我一个无法访问封闭流的错误,System.ObjectDisposedException未处理C# 无效的跨线程访问,c#,windows-phone-7,C#,Windows Phone 7,当我尝试在cam_CaptureImageAvailable方法中使用BitMapImage时,我获得了无效的跨线程访问。我尝试使用Dispatcher,但这给了我一个无法访问封闭流的错误,System.ObjectDisposedException未处理 // Informs when full resolution picture has been taken, saves to local media library and isolated storage. void cam_C
// 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线程上创建
BitmapImage
和WriteableBitmap
,因为它们是DependencyObject
s
出现ObjectDisposedException
错误的原因是,当dispatcher处理您的请求时,映像的流已经关闭
您是否尝试将该代码移动到Dispatcher调用中
e.ImageStream.Close();
dispatcher的代码不会立即执行,而是在稍后的时间点执行,但是您已经关闭了流
如果这不起作用,那么您可以通过在
CaptureImageAvailable
中将流读取到内存中,然后将MemoryStream
作为源传递到BitmapImage
Hi pivotnig,我正在使用内存流,它可以工作,但每次拍照时我只会得到第一张图像,下面是我正在使用的代码codee.ImageStream.Seek(0,SeekOrigin.Begin);e、 ImageStream.CopyTo(毫秒)代码>并且在dispatcher中我有var-bitmapImage=new-bitmapImage();bitmapImage.SetSource(毫秒);WriteableBitmap wb=新的WriteableBitmap(位图图像)代码>谢谢你的帮助我每次拍照都只得到第一张图片没关系我把内存流作为全局变量:)