Merge pull request #2971 from BrzVlad/feature-cross-binprot
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Toolbox / ToolboxItemImageConverter.cs
1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------
4
5 namespace System.Activities.Presentation.Toolbox
6 {
7     using System;
8     using System.Drawing;
9     using System.Globalization;
10     using System.Windows;
11     using System.Windows.Data;
12     using System.Windows.Interop;
13     using System.Windows.Media;
14     using System.Windows.Media.Imaging;
15     using Microsoft.Tools.Common;
16
17     //This class is responsible for converting 'old' bitmap style, contained 
18     //in ToolboxItem objects to WPF compatible ImageSource object
19
20     [ValueConversion(typeof(Bitmap), typeof(ImageSource))]
21     sealed class ToolboxItemImageConverter : IValueConverter
22     {
23         object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
24         {
25             Bitmap source = value as Bitmap;
26             if (targetType == typeof(ImageSource) && null != source)
27             {
28                 IntPtr hBitmap = source.GetHbitmap();
29                 try
30                 {
31                     BitmapSizeOptions sizeOptions = BitmapSizeOptions.FromEmptyOptions();
32                     return Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, sizeOptions);
33                 }
34                 finally
35                 {
36                     Win32Interop.DeleteObject(hBitmap);
37                 }
38             }
39             return null;
40         }
41
42         object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
43         {
44             throw FxTrace.Exception.AsError(new NotSupportedException());
45         }
46     }
47 }