Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 923171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:09:04+00:00 2026-05-15T19:09:04+00:00

I have a data class in my application which maintains a collection of byte

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:09:04+00:00Added an answer on May 15, 2026 at 7:09 pm

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a class that has a member called data which is
Using DBIx::Class and I have a resultset which needs to be filtered by data
Using C# .NET 2.0, I have a composite data class that does have the
I have a class 'Data' that uses a getter to access some array. If
Let's say I have data structures that're something like this: Public Class AttendenceRecord Public
in my data layer class I have created a function to manually refresh the
Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();
I have a data structure that represents C# code like this: class Namespace: string
I have a class in system-C with some data members as such: long double
I have a factory class that populates objects with data. I want to implementing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.