BitmapSource Converted From Icon - CSharp System.Windows.Media.Imaging

CSharp examples for System.Windows.Media.Imaging:BitmapSource

Description

BitmapSource Converted From Icon

Demo Code


using System.Windows.Media.Imaging;
using System.Windows.Interop;
using System.Windows;
using System.Drawing;
using System;//from  w w w .j  a  v a2 s. c  o  m

public class Main{
        public static BitmapSource ConvertFromIcon(Icon icon)
        {

            try
            {
                var bs = Imaging
                    .CreateBitmapSourceFromHIcon(icon.Handle,
                                                 new Int32Rect(0, 0, icon.Width, icon.Height),
                                                 BitmapSizeOptions.FromWidthAndHeight(icon.Width,
                                                                                      icon.Height));
                return bs;
            }
            finally
            {
                DeleteObject(icon.Handle);
            }
        }
}

Related Tutorials