后台ScheduledAgent从流中设置位图图像源时出现内存不足异常

时间:2013-07-03 09:53:55

标签: windows-phone-7 windows-phone-8

我想定期更新Flip Tile Back Image,以便添加ScheduledAgent Project并尝试将图像保存在isolated Share Folder内的固定路径中。但是当我从BitmapImage设置Image Stream来源时,我的内存异常会出现异常。

这是代码

    using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication())
    {
        const string filePath = @"Shared\ShellContent\FlipBackImage.jpg";
        var filename = "Image.png";
        var stream = !isoFile.FileExists(filename) ? null : isoFile.OpenFile(filename, FileMode.Open, FileAccess.Read);
        if (stream != null)
        {
            if (isoFile.FileExists(filePath))
            {
                isoFile.DeleteFile(filePath);
            }
            Debug.WriteLine("currentMemory"+DeviceStatus.ApplicationCurrentMemoryUsage);
            var bi = new BitmapImage();
            bi.CreateOptions = BitmapCreateOptions.None;
            bi.SetSource(stream); //out of memory exception getting here
            var wbm=new WriteableBitmap(bi);
            using (var streamFront = isoFile.OpenFile(filePath, FileMode.Create))
            {
                wbm.SaveJpeg(streamFront, 691, 336, 0, 80);
            }
            Deployment.Current.Dispatcher.BeginInvoke(() => Utils.DisposeImage(wbm));
        }

每次尝试设置Image Resolution 1500x1100时都会收到异常,即使它的大小只是29 kb
如何处理?

1 个答案:

答案 0 :(得分:0)

iso.Close();

一旦图像流读完就写完

相关问题