开发者

WPF Images not rendering from in-memory sources

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)?


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.


Link

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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜