Introduction

This article describes FileToIconConverter, which is a MultiBinding Converter that can retrieve an Icon from system based on a filename(exist or not) and size.

Background

I am working on a file explorer, which shows a file list, inside the filelist, which require to place a file icon next to each file in a folder. In my first implementation, I added a Icon property in the data model of the file, it works fine. When I implements the Icon View, I added a Large Icon property, then when I add Thumbnail property for the Thumbnail view support.

This implementation has a number of problems,

How to use?

The converter is a MultiBinding Converter, which takes 2 parameters, filename and size,

How it works?

MultiBinding Converter is similar to the normal Binding IValueConverter except it takes multiple value to convert, the Convert is like below :

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)

{

int size = defaultSize;

if (values.Length > 1 && values[1] is double)

size = (int)(float)(double)values[1];

if (values[0] is string)

return imageDic[getIconKey(values[0] as string, size)];

else return imageDic[getIconKey("", size)];

}

I try to avoid SystemImageList because it have it's own cache system which will cause some overhead. There are two cache, iconCache and thumbnailCache, all Icons and thumbnail are stored in cache.

1) private void PollThumbnailCallback(object state)

2) {

3) //Non UIThread

4) thumbnailInfo input = state as thumbnailInfo;

5) string fileName = input.fullPath;

6) WriteableBitmap writeBitmap = input.bitmap;

7) Bitmap origBitmap = new Bitmap(fileName);

8) Bitmap inputBitmap = resizeImage(origBitmap, new System.Drawing.Size(256, 256));

9) BitmapSource inputBitmapSource = Imaging.CreateBitmapSourceFromHBitmap(inputBitmap.GetHbitmap(),

A) IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

B) origBitmap.Dispose();

C) inputBitmap.Dispose();

D) int width = inputBitmapSource.PixelWidth;

E) int height = inputBitmapSource.PixelHeight;

F) int stride = width * ((inputBitmapSource.Format.BitsPerPixel + 7) / 8);

G) byte[] bits = new byte[height * stride];

H) inputBitmapSource.CopyPixels(bits, stride, 0);

I) inputBitmapSource = null;

J) writeBitmap.Dispatcher.Invoke(DispatcherPriority.Background,

K) new ThreadStart(delegate

L) {

M) //UI Thread

N) Int32Rect outRect = new Int32Rect(0, (int)(writeBitmap.Height - height) / 2, width, height);

O) writeBitmap.WritePixels(outRect, bits, stride, 0);

P) }));

Q) }

Issues

The component is still very resource hogging. , fixed, I just noticed I have to clear the created object myself using DeleteObject if I called Bitmap.GHbitmap, like below

IntPtr hBitmap = source.GetHbitmap();

return Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

DeleteObject(hBitmap);

References

History

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License.