且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Windows Phone 8的WriteableBitmap的内存泄漏

更新时间:2023-01-12 18:26:37

有关这一点,你必须存储里面的这个文件流IsolatedStorege。因此,使用IsolatedStorageFileStream创建一个文件流,然后将其保存,像这样...

 私人无效photoChooserTaskComplete(对象发件人,PhotoResult E) {
如果(e.TaskResult == TaskResult.OK){
SaveToIsolatedStorage(e.ChosenPhoto,你的文件名);
}



}

 公共无效SaveToIsolatedStorage(流的ImageStream,字符串文件名)
{

{
串imagename =文件名+.JPG;使用
(IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
如果(myIsolatedStorage.FileExists(imagename))
{
myIsolatedStorage.DeleteFile(imagename);
}
IsolatedStorageFileStream FILESTREAM = myIsolatedStorage.CreateFile(imagename);
WriteableBitmap的WB =新WriteableBitmap的(100,100);
wb.SetSource(的ImageStream);
wb.SaveJpeg(FILESTREAM,100,100,0,70);
fileStream.Close();
}
}

赶上(例外)
{
RadMessageBox.Show(的String.Empty,MessageBoxButtons.OK,而保存图像时出错 );
}

}

和阅读你可以得到从IsolatedStorage文件

 公共WriteableBitmap的ReadFromIsolatedStorage(字符串文件名)
{
WriteableBitmap的位图=新的WriteableBitmap的(100 ,100);

{使用
(IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
如果(myIsolatedStorage.FileExists(文件名))
{

使用(IsolatedStorageFileStream FILESTREAM = myIsolatedStorage.OpenFile(文件名,FileMode.Open,FileAccess.Read))
{
//解码的JPEG视频流。
位= PictureDecoder.DecodeJpeg(FILESTREAM,100,100);
}
}
}
}
赶上(例外)
{
RadMessageBox.Show(的String.Empty,MessageBoxButtons.OK错误Occcured同时读取图像);
}


返回位图;
}

这将解决您的内存泄漏问题,试试这个...


I'm having a memory leak whenever I create any instance of a WriteableBitmap. I've tried multiple suggestions on *** and other forums, but nothing is working. The basic flow of my test app is this:

  1. Select an image with the PhotoChooserTask
  2. Use the Stream from the PhotoResult object to create a WriteableBitmap.

That's it. Nulling the variables and calling GC.Collect() only solves part of the problem. It keeps the app from allocating memory until the app crashes, but even though the objects have gone out of scope, there is always memory allocated for them until I select a new image. I can reproduce it with the default Windows Phone Direct3D with XAML App. The only modifications to the default project are the following:

MainPage.xaml.cs

public MainPage() {
    InitializeComponent();
    _photoChooserTask = new PhotoChooserTask();
    _photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTaskComplete);
}

private void ApplicationBarIconButton_Click(object sender, EventArgs e) {
    _photoChooserTask.Show();
}

private void photoChooserTaskComplete(object sender, PhotoResult e) {
    if (e.TaskResult == TaskResult.OK) {
        BitmapImage image = new BitmapImage();
        image.SetSource(e.ChosenPhoto);
        WriteableBitmap wbm = new WriteableBitmap(image);
        image.UriSource = null;
        image = null;
        wbm = null;
        GC.Collect();
    }
}

MainPage.xaml

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" Mode="Default" Opacity="0.5" >
        <shell:ApplicationBar.Buttons>
            <shell:ApplicationBarIconButton IconUri="/junkUrl.png" Text="albums" Click="ApplicationBarIconButton_Click" />
        </shell:ApplicationBar.Buttons>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

For this you have to store this file stream inside the IsolatedStorege. So create a filestream using IsolatedStorageFileStream and then save it, like this...

private void photoChooserTaskComplete(object sender, PhotoResult e) {
if (e.TaskResult == TaskResult.OK) {
       SaveToIsolatedStorage(e.ChosenPhoto,"Your File Name");           
}

}

 public void SaveToIsolatedStorage(Stream imageStream, string fileName)
    {
        try
        {
            string imagename =  fileName + ".jpg";
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myIsolatedStorage.FileExists(imagename))
                {
                    myIsolatedStorage.DeleteFile(imagename);
                }
                IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(imagename);
                WriteableBitmap wb = new WriteableBitmap(100, 100);
                wb.SetSource(imageStream);
                wb.SaveJpeg(fileStream, 100, 100, 0, 70);
                fileStream.Close();
            }
        }

        catch (Exception)
        {
            RadMessageBox.Show(String.Empty, MessageBoxButtons.OK, "Error occured while saving Images");                               
        }

    }

And for reading you can get that file from IsolatedStorage

public WriteableBitmap ReadFromIsolatedStorage(string fileName)
    {
        WriteableBitmap bitmap = new WriteableBitmap(100, 100);
        try
        {
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myIsolatedStorage.FileExists(fileName))
                {

                    using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read))
                    {
                        // Decode the JPEG stream.                            
                        bitmap = PictureDecoder.DecodeJpeg(fileStream, 100, 100);
                    }
                }
            }
        }
        catch (Exception)
        {
            RadMessageBox.Show(String.Empty, MessageBoxButtons.OK, "Error Occcured while reading image");                               
        }


        return bitmap;
    }

This will solved your memory leak problem, try this...