Problem loading BitmapImage in WPF

1,174 views
Skip to first unread message

Laurent Bugnion

unread,
Jan 24, 2011, 11:23:49 AM1/24/11
to wpf-di...@googlegroups.com

Hey guys,

 

I am trying to load a stream from a local file, assign to a BitmapImage, and then later in the application, the file may be moved to another location.

 

I am trying this:

 

            using (var stream = File.OpenRead(fileInfo.FullName))

            {

                duplicate.Image = new BitmapImage

                {

                    CacheOption = BitmapCacheOption.OnLoad

                };

                duplicate.Image.BeginInit();

                duplicate.Image.StreamSource = stream;

                duplicate.Image.EndInit();

            }

 

However the image is not fully loaded (I see a gray rectangle in the UI).

 

I then tried

 

            var stream = File.OpenRead(fileInfo.FullName);

            duplicate.Image = new BitmapImage

            {

                CacheOption = BitmapCacheOption.OnLoad

            };

            duplicate.Image.BeginInit();

            duplicate.Image.StreamSource = stream;

            duplicate.Image.EndInit();

 

(based on the documentation that says that BitmapCacheOption.OnLoad will close the stream after the BitmapImage is created). However, when I try to move the file, I get an error saying that it is used by another process, yadayadayada.

 

What am I missing? (NB: Same thing when I try with UriSource).

 

BTW in XAML I just do

<Image Source="{Binding Image}"

       Height="98"

       Cursor="Hand">

 

Cheers

Laurent

--

Laurent Bugnion

Director, UX Integration Development, IdentityMine

Microsoft MVP, MCP

www.galasoft.ch | blog.galasoft.ch

 

Jeremiah Morrill

unread,
Jan 24, 2011, 12:15:35 PM1/24/11
to wpf-di...@googlegroups.com
While I'm not sure how to fix this specific issue, as it looks like the read of the file stream is happening asynchronously (or sync but later) and not closing the stream.

I would copy the file stream to a byte[], then to a new memory stream.  Close the filestream, then assign the new memory stream to the image.  

-Jer
--
Microsoft MVP - Client Application Development
HJT, Inc Software Developer

Laurent Bugnion

unread,
Jan 24, 2011, 12:19:23 PM1/24/11
to wpf-di...@googlegroups.com

Yeah that was my analysis too. I was hoping to avoid that step…

 

Cheers!

Laurent

Jeremiah Morrill

unread,
Jan 24, 2011, 12:36:36 PM1/24/11
to wpf-di...@googlegroups.com
Yeah if you do have to go the MemoryStream route, don't forget to set the Position to 0.  That still gets me every time!

-Jer

Peter O'Hanlon

unread,
Jan 24, 2011, 3:36:56 PM1/24/11
to wpf-di...@googlegroups.com
I like to use code like this:

Uri uri = new Uri(filename, UriKind.Absolute);
img = new BitmapImage();
img.BeginInit();
img.CreateOption = BitmapCreateOptions.IgnoreImageCache;
img.CacheOption = BitmapCacheOption.OnLoad;
img.UriSource = uril
img.EndInit();

image.Source = img;

Regards

Pete
--
Peter O'Hanlon

Sacha Barber

unread,
Jan 25, 2011, 1:57:14 AM1/25/11
to wpf-di...@googlegroups.com
I do it how Pete does it also
--
Sacha Barber
sacha....@gmail.com
Reply all
Reply to author
Forward
0 new messages