[英]Windows phone 8 Bitmap allocation crashing everything in my camera app
I'm having a weird memory allocation problem with my windows phone 8 application. 我的Windows Phone 8应用程序有一个奇怪的内存分配问题。
What I'm trying to achieve is pretty simple, take a picture, save it to the isolated storage, then add it to a collection of pictures in my datacontext. 我要实现的目标非常简单,拍摄一张照片,将其保存到隔离的存储中,然后将其添加到我的数据上下文中的一组照片中。
Context of application is I have an appointment during which I take pictures, then when I get back to my appointment in my in-app calendar, i get to see all pictures related to the appointment. 应用程序的上下文是我有一个约会期间要拍照,然后在我的应用内日历中返回约会时,可以看到与约会相关的所有图片。
I have three pages, a calendar page, when i click on an appointment, i get to a detail view of the appointment, and in it i have a button sending me to a camera application (mine, not the one from the phone). 我有三个页面,一个日历页面,当我单击一个约会时,会进入该约会的详细视图,在其中有一个按钮,将我发送到相机应用程序(我的不是手机上的那个)。 The following code is in my camera.xaml.cs file. 以下代码在我的camera.xaml.cs文件中。
Here's the faulty code, in the camera page: 在相机页面中,这是错误的代码:
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);
The following line crash the program: 以下行使程序崩溃:
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);
Rest of the code works fine (no crash if I comment the instructions above. 其余代码工作正常(如果我评论以上说明,不会崩溃。
// 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();
}
I honestly have no idea why this is crashing... Would gladly take any suggestions. 老实说,我不知道为什么会崩溃...很乐意接受任何建议。
Ok sorry for the non answered post but I figured it out. 好的,对于未回答的帖子,我们感到抱歉,但我知道了。
It was a threading problem, my bitmap creation being in the UI thread. 这是一个线程问题,我的位图创建在UI线程中。 By using a Task.Run(()=>bitmap_creation) everything works fine. 通过使用Task.Run(()=> bitmap_creation),一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.