C# Windows phone 8位图分配使我的相机应用程序中的所有内容崩溃
我的windows phone 8应用程序有一个奇怪的内存分配问题 我试图实现的是非常简单的,拍摄一张图片,将其保存到独立的存储中,然后将其添加到我的datacontext中的图片集合中 应用程序的上下文是我有一个约会,在此期间我会拍照,然后当我在应用程序内日历中返回到我的约会时,我会看到与约会相关的所有照片 我有三个页面,一个日历页面,当我点击一个约会时,我会看到约会的详细信息,在其中我有一个按钮将我发送到一个摄像头应用程序(我的,不是手机上的那个)。以下代码位于my camera.xaml.cs文件中 以下是摄像头页面中的错误代码:C# Windows phone 8位图分配使我的相机应用程序中的所有内容崩溃,c#,windows-phone-8,C#,Windows Phone 8,我的windows phone 8应用程序有一个奇怪的内存分配问题 我试图实现的是非常简单的,拍摄一张图片,将其保存到独立的存储中,然后将其添加到我的datacontext中的图片集合中 应用程序的上下文是我有一个约会,在此期间我会拍照,然后当我在应用程序内日历中返回到我的约会时,我会看到与约会相关的所有照片 我有三个页面,一个日历页面,当我点击一个约会时,我会看到约会的详细信息,在其中我有一个按钮将我发送到一个摄像头应用程序(我的,不是手机上的那个)。以下代码位于my camera.xaml.
public void cam_CaptureThumbnailAvailable(object sender, ContentReadyEventArgs e)
{
string fileName;
string imageName = "PHOTO_" +
DateTime.Now.ToString("yyyy-dd-MM-hh-mm") +
"_" +
_savedcounter +
"_th.jpg";
if ((App.Current as App).CurrentAppointment != null
&&
!string.IsNullOrEmpty((App.Current as App).CurrentAppointment.Reference))
{
fileName = (App.Current as App).CurrentAppointment.Reference +
"\\" +
imageName;
}
else
{
fileName = _savedcounter + "_th.jpg";
}
try
{
//Making sure I'm at the beginning of the stream
e.ImageStream.Seek(0, SeekOrigin.Begin);
以下行使程序崩溃:
BitmapImage bi = new BitmapImage(); //This line.
bi.SetSource(e.ImageStream);
WriteableBitmap wb = new WriteableBitmap(bi);
int index = (App.Current as App).CurrentAppointment.Pictures.Count - 1;
MyPicture newPic = new MyPicture(wb,
imageName,
index,
false
);
e.ImageStream.Seek(0, SeekOrigin.Begin);
代码的其余部分工作正常(如果我对上面的说明进行注释,则不会崩溃)
// Save thumbnail as JPEG to the local folder.
using (IsolatedStorageFile isoStore =
IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream targetStream =
isoStore.OpenFile(
fileName,
FileMode.Create,
FileAccess.Write
)
)
{
// Initialize the buffer for 4KB disk pages.
byte[] readBuffer = new byte[4096];
int bytesRead = -1;
// Copy the thumbnail to the local folder.
while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
{
targetStream.Write(readBuffer, 0, bytesRead);
}
}
}
}
finally
{
// Close image stream
e.ImageStream.Close();
}
我真的不知道为什么会崩溃……我很乐意接受任何建议。好的,很抱歉没有回复帖子,但我找到了答案
这是一个线程问题,我的位图创建在UI线程中。通过使用Task.Run(()=>bitmap\u创建),一切正常。您遇到了什么异常?