1

I am trying to use the WriteableBitmap object because I need it for rotating images and saving images to the Isolated Storage of my app.

The problem is, it uses so much memory it eventually causes an out of memory exception.

Here is a picture of the memory usage of my App, with the picture link here for better viewing.

Memory Chart

Here's an instance of where I use a WriteableBitmap:

        WriteableBitmap picture = new WriteableBitmap(PictureCanvas, null);

        using (var memoryStream = new MemoryStream())
        {
            picture.SaveJpeg(memoryStream, picture.PixelWidth, picture.PixelHeight, 0, 100);

            using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(Globals.OVERLAY_FILE_NAME, FileMode.Create, myIsolatedStorage))
                {
                    fileStream.Write(memoryStream.ToArray(), 0, memoryStream.ToArray().Length);
                    fileStream.Close();
                }
            }
        }

        picture = picture.Crop(0, 0, 1, 1);

I try cropping the image to make it take up less memory, but that doesn't do anything.

I am using the WriteableBitmap extensions library here and in the front page it mentions a Dispose() method, but I don't see it in my App.

If someone could please tell me how to get around this problem or point me somewhere I can find a possible solution, that would be awesome!

Community
  • 1
  • 1
Chris Sippel
  • 111
  • 1
  • 2
  • 9

1 Answers1

1

I'm having a similar issue and still investigating but at least a small tip I can give : if possible get rid of the MemoryStream and write directly to the fileStream like so :

    WriteableBitmap picture = new WriteableBitmap(PictureCanvas, null);
    using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
    using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(Globals.OVERLAY_FILE_NAME, FileMode.Create, myIsolatedStorage))
    {
        picture.SaveJpeg(fileStream, picture.PixelWidth, picture.PixelHeight, 0, 100)
    }

This should buy you some memory.

Community
  • 1
  • 1
Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166
  • I was able to reduce my memory usage by setting the writeableBitmap to null. Now I didn't include this in my question, but I was getting a memory leak from a video brush, and that was actually causing my leak. I would suggest the setting to null. I also looked at your code and you dispose of the streams you make, but don't close them. Maybe that will help? I would also suggest making a small app that uses just a writeableBitmap to make an image then check the memory usage and keep building on your small sample until you find your leak. – Chris Sippel Oct 16 '13 at 22:56
  • Thanks Chris! I glanced over the nulling of the brush although I did notice it. Disposing of a stream automatically closes it, check it out using Reflector, IL Spy or any other decompiling tool. That is redundant. – Andrei Rînea Oct 17 '13 at 06:45
  • However, please try this thing, to write directly to the fileStream and avoiding the MemoryStream. The MemoryStream uses internally byte arrays which get reallocated a lot (doubled each time they're overflown) and generate a lot of garbage. In the end I see no purpose to it.. – Andrei Rînea Oct 17 '13 at 06:46