Speeding up image loading in WPF using thumbnails

Source: Internet
Author: User

Technorati tags: WPF, thumbnails, image, performance, slow, bitmapimage

 

During a recent WPF Session I needed to build a ListBox that showed a bunch of images loaded from an arbitrary directory. thanks to WPF's data binding, this was trivial-I just needed to get a collection of objects that each had a property pointing to the full path of the image, and WPF wocould take care of all the loading/displaying. something like this:

   1: <ListView ItemsSource="{Binding}">
   2:     <ListView.ItemTemplate>
   3:         <DataTemplate>
   4:             <Image Source="{Binding Path=FullPath}" />
   5:         </DataTemplate>
   6:     </ListView.ItemTemplate>
   7: </ListView>

The assumption here is that the datacontext for this window is set to a collection of "photo" objects. the photo class has a member called "fullpath" which is just a string with the full path of the photo on disk-this is what the image. source member expects.

This worked, but it didn't take long to see a major issue: With today's cameras, loading multiple 5 + megapixel images cocould take a while (not to mention the ram requirements ).

After a little digging, I found a solution. there exists a feature in the bitmapimage class that allows you to load an image but tell it to only load a thumbnail. to use this, you have to step out of the shrink-wrapped Data Binding world and insert a converter into the equation. basically, this converter will take the above string with the full image path, it will load the image (as a thumbnail), and pass it back intoImage. SourceParameter as abitmapimage, which it's happy to consume.

First, let's look at this converter's code and how it loads the thumbnail:

   1: public class UriToBitmapConverter : IValueConverter
   2: {
   3:     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
   4:     {
   5:         BitmapImage bi = new BitmapImage();
   6:         bi.BeginInit();
   7:         bi.DecodePixelWidth = 100;
   8:         bi.CacheOption = BitmapCacheOption.OnLoad;
   9:         bi.UriSource = new Uri( value.ToString() );
  10:         bi.EndInit();
  11:         return bi;
  12:     }
  13:  
  14:     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  15:     {
  16:         throw new Exception("The method or operation is not implemented.");
  17:     }
  18: }

Notice line #7. that's the magic line which tells it how big of a thumbnail to load. the smaller the number, the quicker the load, the lower the quality. notice also line #8-this is there to force the image file to be closed after it's loaded. without that, I found that my app couldn't write back to the image file since The ListBox still had it open.

Next, let's look at the XAML change to insert this converter into the mix. You'll need to create a resource for it:

   1: <Window.Resources>
   2:     <local:UriToBitmapConverter x:Key="UriToBitmapConverter" />
   3: </Window.Resources>

The "Local:" namespace directive on line #2 is one I 'd made sure to add to my main "window" declaration, like this (line #4 ):

   1: <Window x:Class="MyClass.Demo"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     xmlns:local="clr-namespace:MyClass"
   5:     Title="Demo" Height="300" Width="300">

Lastly, use this new resource inImageElement so thatFullpath(A string) gets pushed through the converter beforeImage. SourceGets it:

   1: <Image Source="{Binding Path=FullPath, Converter={StaticResource UriToBitmapConverter}}" />

That's it. Your images will now load very quickly. Tweak the thumbnail size to vary the speed versus quality.

Avi

Speeding up image loading in WPF using thumbnails

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.