ToolboxItemImageConverter.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / Tools / System.Activities.Presentation / System / Activities / Presentation / Toolbox / ToolboxItemImageConverter.cs / 1484997 / ToolboxItemImageConverter.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------

namespace System.Activities.Presentation.Toolbox 
{
    using System; 
    using System.Drawing; 
    using System.Globalization;
    using System.Windows; 
    using System.Windows.Data;
    using System.Windows.Interop;
    using System.Windows.Media;
    using System.Windows.Media.Imaging; 

    //This class is responsible for converting 'old' bitmap style, contained 
    //in ToolboxItem objects to WPF compatible ImageSource object 

    [ValueConversion(typeof(Bitmap), typeof(ImageSource))] 
    sealed class ToolboxItemImageConverter : IValueConverter
    {
        object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
        { 
            Bitmap source = value as Bitmap;
            if (targetType == typeof(ImageSource) && null != source) 
            { 
                IntPtr hBitmap = source.GetHbitmap();
                try 
                {
                    BitmapSizeOptions sizeOptions = BitmapSizeOptions.FromEmptyOptions();
                    return Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, sizeOptions);
                } 
                finally
                { 
                    System.Activities.Presentation.View.Win32Interop.DeleteObject(hBitmap); 
                }
            } 
            return null;
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
        {
            throw FxTrace.Exception.AsError(new NotSupportedException()); 
        } 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK