简体   繁体   English

Windows Phone 8位图分配使我的相机应用程序崩溃

[英]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.

相关问题 Windows Phone App崩溃无一例外 - Windows Phone App crashing without exceptions 每次在Windows Phone中Flashlight应用程序崩溃 - Flashlight App crashing every time in Windows Phone Windows Phone 7应用程序崩溃但调试时不崩溃 - Windows Phone 7 App crashing but not when debugging 我的Windows Phone应用在启动前一直崩溃,我到目前为止只做过ui - My windows phone app keeps crashing before startup and I have only done the ui so far 相机镜头应用程序导航在Windows Phone 8中不起作用 - Camera Lens app navigation is not working in Windows Phone 8 Windows Phone 8摄像头基本应用中的InvalidOperationException - InvalidOperationException in a basic windows phone 8 camera app 有没有办法从Windows Phone 8.1应用程序启动手机摄像头应用程序 - Is there a way to launch phone camera app from Windows Phone 8.1 app 如何在Windows 8手机应用程序中镜像前置摄像头上的图像/预览 - How to mirror image/preview coming on front camera in my windows 8 phone app 如何从我的Windows Phone 8应用程序(XAML和C#)访问相机并将拍摄的照片保存在确定的文件夹中? - How to access the camera from my Windows Phone 8 app (XAML and C#) and save the taken picture in a determined folder? Windows Phone Control的位图 - Bitmap of Windows Phone Control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM