Windows phone 8的位图分配导致我的相机应用程序崩溃

本文关键字:我的 相机 应用程序 崩溃 分配 phone 位图 Windows | 更新日期: 2023-09-27 17:53:46

我的windows phone 8应用程序有一个奇怪的内存分配问题。

我想要实现的很简单,拍摄一张照片,将其保存到隔离的存储中,然后将其添加到我的数据上下文中的图片集合中。

应用程序的上下文是我有一个约会,在此期间我拍照,然后当我回到我的应用程序内日历中的约会时,我可以看到与约会相关的所有照片。

我有三个页面,一个日历页面,当我点击一个约会时,我进入约会的详细视图,在其中我有一个按钮将我发送到相机应用程序(我的,不是手机上的)。下面的代码在我的camera.xaml.cs文件中。

这里是错误的代码,在相机页面:

 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();
        }

我真的不知道为什么会崩溃…

Windows phone 8的位图分配导致我的相机应用程序崩溃

好吧,抱歉没有回复,但我明白了。

这是一个线程问题,我的位图创建在UI线程。通过使用Task.Run(()=>bitmap_creation),一切工作正常