locked
Images with WriteableBitmap source disappear

    Question

  • I'm using a WriteableBitmap as a source for Images displayed in my XAML UI. I followed instructions in other forum threads to write the pixel contents in the WriteableBitmap using the IBufferByteAccess COM interface. This all works fine, and my images appear as expected. However, when I switch away from my app and launch other apps, then switch back to my app, all of my images disappear.

    I suspect what is happening is that the IBufferByteAccess keeps the image locked, and DirectX is forcing the locked buffer to be given up when it needs video memory for other apps.

    Incidentally, other apps appear to be suffering from this behavior as well. Here's a repro. Launch the built-in Windows 8 "Photos" app and view a photo at full resolution. Then switch away from the app and launch another app that makes use of images (e.g. the built-in "News" app). Now switch back to the Photos app and watch the photo image turn all black.

    How do I work around this issue? If my theory is correct, it would seem that the IBufferByteAccess is incomplete and requires a way to allow callers to lock and unlock the underlying buffer.

     

    For reference, here's the code I'm using to fill in the WriteableBitmap (with error handling removed for brevity):

     auto wb = ref new WriteableBitmap(1000, 1000);

     ComPtr<IUnknown> unknown(reinterpret_cast<IUnknown*>(
      wb->PixelBuffer));
     
     ComPtr<IBufferByteAccess> bufferByteAccess;
     unknown.As(&bufferByteAccess);

     byte* pixelSource = nullptr;
     bufferByteAccess->Buffer(&pixelSource);

     // Fill entire image with blue pixels.
     for (uint32 row = 0; row < 1000; row++)
     {
      for (uint32 col = 0; col < 1000; col++)
      {
       pixelSource[0] = 0xFF;
       pixelSource += 4;
      }
     }

      MyImage->Source = wb;

     

    Wednesday, December 05, 2012 2:17 AM

All replies

  • Hi Rick,

    This sounds like it might be a video driver problem and likely isn't something you can fix in your code. I cannot reproduce this with the Photos and News app on my machine, but since neither of these use Xaml WriteableBitmap this test suggests that it isn't the problem.

    I'd see if there is a newer driver available for your video card.

    --Rob

    Wednesday, December 05, 2012 2:28 AM
    Owner
  • I have exactly the same problem. My images disappear after I use other apps. Not always. And in different time intervals.

    I need to find a solution. I have already submitted the app, after I noticed the problem. Now I have stopped the review.

    Only images disappear where I've set the source by WriteableBitmap. Images that are set by the designer from the resources are still there. The WriteableBitmap objects are also still there at the moment of disappearance.


    EDIT: Perhaps it is important to know that I am using C#.
    • Edited by BACHELOR_VS Thursday, December 20, 2012 3:39 PM
    Thursday, December 20, 2012 9:54 AM
  • This issue can also be seen in the app "Photo". Open a photo, zoom in a bit. Go to a 3D game with video sequences, play half an hour. Return back to the app "Photo"and suddenly the picture disappears.

    Can I do somthing in my own app against that issue?

    Thursday, December 20, 2012 5:18 PM
  • I have investigated a little. It always happens when a lot of apps use a lot of memory. I'm testing on a Surface.

    If too much memory is used, then the OS released somehow memory in all inactive, but running apps. Which makes the images disappear. When I look at the source of the images, it is not null. It's all ok, but the images are not visible.

    I have tried image.Visibility = Visible; image.InvalidateArrange(); image.UpdateLayout(); Does not help anything.

    I have tried that after the disappear of the image:    image.source = oldWriteableBitmap.
    Does not help anything.


    It helps when I re-creat the WriteableBitmap and set the source of the image. Then it can be seen again.

    The problem is that I can not determine whether the image is disappear.
    Is there something like "memory warning" event???

    Can someone help me? Until I had solved this problem, I can not put the app in the store.


    • Edited by BACHELOR_VS Friday, December 21, 2012 5:50 PM
    Friday, December 21, 2012 5:49 PM
  • Any news on this probem? I have the same problem.

    In my case, I use WriteableBitmap inside a GridView.

    When I set the GridView to Collapsed, then switch the app, wait until it is suspended, return to the app and then make the GridView Visible, then the Images are gone away.

    What should we do? This is a serious problem!

    Sunday, June 09, 2013 4:15 PM
  • Hi!

    I found the answer of the problem on StackOverflow:

    http://stackoverflow.com/questions/14433370/why-do-my-images-sometimes-reload-and-writeablebitmaps-disappear-on-resume?rq=1

    It is the answer by "William Custode".

    Just call Invalidate() for your WriteableBitmaps. At least, this works for me.

    Monday, June 10, 2013 7:18 AM