tags:

views:

58

answers:

2

I have a data class in my application which maintains a collection of byte arrays representing JPEG images. It's defined as:

private ArrayList FrameList = new ArrayList();

I'm having some troubles with my Image object rendering a blank page (and taking its sweet time to do it as well). When I insert a blank image with a 2K in-memory byte array (byte x[] = { lots of hex values };):

FrameList.Insert(currFrame, x);

and then import a JPEG file over it later on with:

byte[] bytes = File.ReadAllBytes(fspec);
FrameList[currFrame] = bytes;

the array is read correctly into memory and stored in the ArrayList (confirmed with the debugger).

However,I then have a function to get the image:

public BitmapImage getCurrPicture()
{
    MemoryStream strm;
    BitmapImage bmp = new BitmapImage();
    strm = new MemoryStream((byte[])FrameList[currFrame-1]);
    bmp.CacheOption = BitmapCacheOption.None;
    bmp.BeginInit();
    bmp.StreamSource = strm;
    bmp.EndInit();
    strm.Close();
    return bmp;
}

which is called:

imgPicB.Source = data.getCurrPicture();

and it doesn't always render.

imgPicB is defined in my XAML as:

<Image x:Name="imgPicB"
       Width="400"
       Height="300"
       Stretch="Fill"
       VerticalAlignment="Top" />

Funny thing is, if I use the exact same JPEG setting the source with setting the source to the file URI directly, it renders fine.

Is there some problem with using in-memory JPEG images in WPF? Is there some extra smarts performed when loading from a file (say auto-detection of the image type)?

+1  A: 

Try this:

public BitmapSource GetCurrPicture()
{
    var bitmapImage = new BitmapImage();
    using (Stream stream = new MemoryStream((byte[])FrameList[currFrame-1]))
    {
        bitmapImage.BeginInit();
        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.StreamSource = stream;
        bitmapImage.EndInit();
        bitmapImage.Freeze();
        return bitmapImage;
    }
}

This works by having WPF decode the image immediately with OnLoad, and then release the reference to the stream when it is done. This means the stream can be garbage collected when it leaves the function. Otherwise, BitmapImage could hold onto the stream until it is rendered.

In the code posted in the question, the render would fail because:

  • the decode is delayed; and
  • the stream has been closed at the point where the decode tries to happen.
Darin Dimitrov
Okay, I'll give it a shot, but _why?_ Is it the using or the freeze that you think will help?
paxdiablo
No, it's the `BitmapCacheOption.OnLoad`. As for the `using` it's simply good practice.
Darin Dimitrov
Thanks, Darin, that works perfectly. The thing is, however, I'm still not sure why. Is it because there's no backing "file" for an in-memory stream, especially since I'm closing the stream after creating the bitmap?
paxdiablo
Did this answer your question? http://stackoverflow.com/questions/1733730/load-image-into-memory-immediately Load image into memory immediately
rwong
@paxdiablo: you answered your own question. <br> Darin's solution makes sure that WPF will decode the image immediately (OnLoad) and release the reference to the stream when it is done. This means the stream can be garbage collected when it leaves the function. Otherwise, BitmapImage can hold onto the stream until it is rendered. In your code, the render would fail because the stream had been closed.
rwong
Okay, now I understand. I might just add that into the answer and, if you want to clean it up, @Darin, feel free.
paxdiablo
+1  A: 

http://blogs.msdn.com/b/dwayneneed/archive/2008/06/20/implementing-a-custom-bitmapsource.aspx

DwayneNeed 20 Jun 2008 5:11 PM Comments

  • Caching
    BitmapImage will store the decoded bitmap in system memory. You can control when this happens by setting the CacheOption property. BitmapImage also maintains a cache of previous BitmapImage instances (via weak references) so that loading the same Uri multiple times will share the same instance. To avoid this cache, you can include the BitmapCreateOptions.IgnoreImageCache flag in the CreateOptions property.
rwong