cam_CaptureImageAvailableメソッドでBitMapImageを使用しようとすると、無効なクロススレッドアクセスが発生します。ディスパッチャーを使用しようとしましたが、クローズドストリームにアクセスできませんというエラーが発生しました。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
}
});
誰かが私を助けてくれますか?本当にありがとうございます。