Exemple #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()));
     }
 }
 public static BitmapSource ToBitmapSource(this Bitmap source)
 {
     using (var handle = new SafeHBitmapHandle(source))
     {
         return Imaging.CreateBitmapSourceFromHBitmap(handle.DangerousGetHandle(),
             IntPtr.Zero, Int32Rect.Empty,
             BitmapSizeOptions.FromEmptyOptions());
     }
 }
Exemple #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);
                 }
 }
        /// <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()));
            }
        }
        /// <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()));
            }
        }
        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);
        }
        /// <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;
            }
        }
Exemple #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);
        }
    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);
        }
    }
        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;
        }
        // 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;
            }
        }
 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());
     }
 }