コード例 #1
0
 private BitmapSource ToBitmapSource(System.Drawing.Bitmap source)
 {
     using (var handle = new SafeHBitmapHandle(source))
     {
         return(Imaging.CreateBitmapSourceFromHBitmap(handle.DangerousGetHandle(),
                                                      IntPtr.Zero, Int32Rect.Empty,
                                                      BitmapSizeOptions.FromEmptyOptions()));
     }
 }
コード例 #2
0
ファイル: IconHelper.cs プロジェクト: gmlohaels/fuzzyLauncher
 public static BitmapSource ToBitmapSource(this Bitmap source)
 {
     using (var handle = new SafeHBitmapHandle(source))
     {
         return Imaging.CreateBitmapSourceFromHBitmap(handle.DangerousGetHandle(),
             IntPtr.Zero, Int32Rect.Empty,
             BitmapSizeOptions.FromEmptyOptions());
     }
 }
コード例 #3
0
 /// <summary>
 ///     Bitblt extension for the graphics object
 /// </summary>
 /// <param name="target">Graphics</param>
 /// <param name="sourceBitmap">Bitmap</param>
 /// <param name="source">Rectangle</param>
 /// <param name="destination">Point</param>
 /// <param name="rasterOperations">RasterOperations</param>
 public static void BitBlt(this Graphics target, Bitmap sourceBitmap, Rectangle source, Point destination, RasterOperations rasterOperations)
 {
     using (var targetDc = target.GetSafeDeviceContext())
         using (var safeCompatibleDcHandle = CreateCompatibleDC(targetDc))
             using (var hBitmapHandle = new SafeHBitmapHandle(sourceBitmap.GetHbitmap()))
                 using (safeCompatibleDcHandle.SelectObject(hBitmapHandle))
                 {
                     BitBlt(targetDc, destination.X, destination.Y, source.Width, source.Height, safeCompatibleDcHandle, source.Left, source.Top, rasterOperations);
                 }
 }
コード例 #4
0
        /// <summary>
        ///     Convert a Bitmap to a BitmapSource
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns>BitmapSource</returns>
        public static BitmapSource ToBitmapSource(this Bitmap bitmap)
        {
            if (bitmap == null)
            {
                throw new ArgumentNullException(nameof(bitmap));
            }

            using (var hBitmap = new SafeHBitmapHandle(bitmap.GetHbitmap()))
            {
                return(Imaging.CreateBitmapSourceFromHBitmap(hBitmap.DangerousGetHandle(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()));
            }
        }
コード例 #5
0
        /// <summary>
        /// Convert an Icon to an ImageSource
        /// </summary>
        /// <param name="icon"></param>
        /// <returns></returns>
        public static BitmapSource ToImageSource(this Icon icon)
        {
            var bitmap = icon.ToBitmap();

            using (var hBitmapHandle = new SafeHBitmapHandle(bitmap.GetHbitmap()))
            {
                return(Imaging.CreateBitmapSourceFromHBitmap(
                           hBitmapHandle.DangerousGetHandle(),
                           IntPtr.Zero,
                           Int32Rect.Empty,
                           BitmapSizeOptions.FromEmptyOptions()));
            }
        }
コード例 #6
0
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string sourceFilePath = value as string;

            var source = IconExtractor.GetIcon(sourceFilePath, true).ToBitmap();

            BitmapSource bitSrc = null;

            using (var hBitmap = new SafeHBitmapHandle(source.GetHbitmap(), true))
            {
                bitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap.DangerousGetHandle(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            }

            return(bitSrc);
        }
コード例 #7
0
        /// <summary>
        ///     Transforms a Bitmap to a BitmapSource
        /// </summary>
        /// <param name="source">Bitmap to be transformed</param>
        /// <param name="key">String Key used for identifing the image (caching)</param>
        /// <returns>BitmapSource equivalent to the Bitmap-Input</returns>
        public static BitmapSource ToBitmapSource(this Bitmap source, string key)
        {
            if (CachedBitmapSources.TryGetValue(key, out BitmapSource value))
            {
                return value;
            }

            using (var handle = new SafeHBitmapHandle(source))
            {
                var bitMapSource = Imaging.CreateBitmapSourceFromHBitmap(handle.DangerousGetHandle(),
                    IntPtr.Zero, Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());

                CachedBitmapSources.Add(key, bitMapSource);

                return bitMapSource;
            }
        }
コード例 #8
0
        public static BitmapSource GetWPFBitmap(this IntPtr dibBitmap)
        {
            using (var bm = GetDrawingBitmap(dibBitmap))
            {
                if (bm != null)
                {
                    using (var hbm = new SafeHBitmapHandle(bm.GetHbitmap(), true))
                    {
                        return(System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                                   hbm.DangerousGetHandle(),
                                   IntPtr.Zero,
                                   System.Windows.Int32Rect.Empty,
                                   BitmapSizeOptions.FromEmptyOptions()));
                    }
                }
            }

            return(null);
        }
コード例 #9
0
    public static BitmapSource ToBitmapSource(this Bitmap source)
    {
        try {
            using (var handle = new SafeHBitmapHandle(source)) {
                return                 /*System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                                        *  source.GetHbitmap(Color.Red),
                                        *  IntPtr.Zero,
                                        *  System.Windows.Int32Rect.Empty,
                                        *  BitmapSizeOptions.FromWidthAndHeight(source.Width, source.Height));*/
                       (Imaging.CreateBitmapSourceFromHBitmap(handle.DangerousGetHandle(),
                                                              IntPtr.Zero, Int32Rect.Empty,
                                                              BitmapSizeOptions.FromEmptyOptions()));
            }
        }catch (Exception e) {
            Console.WriteLine(e.Message);
            Console.WriteLine(e.StackTrace);

            return(null);
        }
    }
コード例 #10
0
        public static BitmapSource GetWPFBitmap(this IntPtr dibBitmap)
        {
            using (var bm = GetDrawingBitmap(dibBitmap))
            {
                if (bm != null)
                {
                    using (var hbm = new SafeHBitmapHandle(bm.GetHbitmap(), true))
                    {
                        return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                           hbm.DangerousGetHandle(),
                           IntPtr.Zero,
                           System.Windows.Int32Rect.Empty,
                           BitmapSizeOptions.FromEmptyOptions());
                    }
                }
            }

            return null;
        }
コード例 #11
0
        // My take on this built from a number of
        // resources.
        //   http://stackoverflow.com/a/7035036
        //   http://stackoverflow.com/a/1470182/360211
        //
        //public static BitmapSource ToBitmapSource(this Bitmap source)
        public static BitmapSource GetBitmapSourceWithHBitmap(Bitmap source)
        {
            using (var handle = new SafeHBitmapHandle(source))
            {
                BitmapSource bs = Imaging.CreateBitmapSourceFromHBitmap(handle.DangerousGetHandle(),
                    IntPtr.Zero, Int32Rect.Empty,
                    BitmapSizeOptions.FromWidthAndHeight(source.Width, source.Height));

                //BitmapSource bs = Imaging.CreateBitmapSourceFromHBitmap(handle.DangerousGetHandle(),
                //    IntPtr.Zero, Int32Rect.Empty,
                //    BitmapSizeOptions.FromEmptyOptions());

                return bs;
            }
        }
コード例 #12
0
 public static BitmapSource ConvertBitmapToBitmapSourceBySaveHandle(Bitmap bitmap)
 {
     using (var handle = new SafeHBitmapHandle(bitmap))
     {
         return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(handle.DangerousGetHandle(),
             IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
     }
 }