コード例 #1
0
        /// <summary>
        /// Use the GDI+ blur effect on the bitmap
        /// </summary>
        /// <param name="destinationBitmap">Bitmap to apply the effect to</param>
        /// <param name="area">Rectangle to apply the blur effect to</param>
        /// <param name="radius">0-255</param>
        /// <param name="expandEdges">bool true if the edges are expanded with the radius</param>
        /// <returns>false if there is no GDI+ available or an exception occured</returns>
        public static bool ApplyBlur(Bitmap destinationBitmap, Rectangle area, int radius, bool expandEdges)
        {
            if (!isBlurPossible(radius))
            {
                return(false);
            }
            IntPtr hBlurParams = IntPtr.Zero;
            IntPtr hEffect     = IntPtr.Zero;

            try {
                // Create a BlurParams struct and set the values
                BlurParams blurParams = new BlurParams();
                blurParams.Radius      = radius;
                blurParams.ExpandEdges = expandEdges;

                // Allocate space in unmanaged memory
                hBlurParams = Marshal.AllocHGlobal(Marshal.SizeOf(blurParams));
                // Copy the structure to the unmanaged memory
                Marshal.StructureToPtr(blurParams, hBlurParams, false);

                // Create the GDI+ BlurEffect, using the Guid
                int status = GdipCreateEffect(BlurEffectGuid, out hEffect);

                // Set the blurParams to the effect
                GdipSetEffectParameters(hEffect, hBlurParams, (uint)Marshal.SizeOf(blurParams));

                // Somewhere it said we can use destinationBitmap.GetHbitmap(), this doesn't work!!
                // Get the private nativeImage property from the Bitmap
                IntPtr hBitmap = GetNativeImage(destinationBitmap);

                // Create a RECT from the Rectangle
                RECT rec = new RECT(area);
                // Apply the effect to the bitmap in the specified area
                GdipBitmapApplyEffect(hBitmap, hEffect, ref rec, false, IntPtr.Zero, 0);

                // Everything worked, return true
                return(true);
            } catch (Exception ex) {
                isBlurEnabled = false;
                LOG.Error("Problem using GdipBitmapApplyEffect: ", ex);
                return(false);
            } finally {
                try {
                    if (hEffect != IntPtr.Zero)
                    {
                        // Delete the effect
                        GdipDeleteEffect(hEffect);
                    }
                    if (hBlurParams != IntPtr.Zero)
                    {
                        // Free the memory
                        Marshal.FreeHGlobal(hBlurParams);
                    }
                } catch (Exception ex) {
                    isBlurEnabled = false;
                    LOG.Error("Problem cleaning up ApplyBlur: ", ex);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Draw the image on the graphics with GDI+ blur effect
        /// </summary>
        /// <returns>false if there is no GDI+ available or an exception occured</returns>
        public static bool DrawWithBlur(Graphics graphics, Bitmap image, Rectangle source, Matrix transform, ImageAttributes imageAttributes, int radius, bool expandEdges)
        {
            if (!isBlurPossible(radius))
            {
                return(false);
            }

            IntPtr hBlurParams = IntPtr.Zero;
            IntPtr hEffect     = IntPtr.Zero;

            try {
                // Create a BlurParams struct and set the values
                BlurParams blurParams = new BlurParams();
                blurParams.Radius = radius;
                //blurParams.Padding = radius;
                blurParams.ExpandEdges = false;

                // Allocate space in unmanaged memory
                hBlurParams = Marshal.AllocHGlobal(Marshal.SizeOf(blurParams));
                // Copy the structure to the unmanaged memory
                Marshal.StructureToPtr(blurParams, hBlurParams, true);

                // Create the GDI+ BlurEffect, using the Guid
                int status = GdipCreateEffect(BlurEffectGuid, out hEffect);

                // Set the blurParams to the effect
                GdipSetEffectParameters(hEffect, hBlurParams, (uint)Marshal.SizeOf(blurParams));

                // Somewhere it said we can use destinationBitmap.GetHbitmap(), this doesn't work!!
                // Get the private nativeImage property from the Bitmap
                IntPtr hBitmap     = GetNativeImage(image);
                IntPtr hGraphics   = GetNativeGraphics(graphics);
                IntPtr hMatrix     = GetNativeMatrix(transform);
                IntPtr hAttributes = GetNativeImageAttributes(imageAttributes);

                // Create a RECT from the Rectangle
                RECTF sourceRECF = new RECTF(source);
                // Apply the effect to the bitmap in the specified area
                GdipDrawImageFX(hGraphics, hBitmap, ref sourceRECF, hMatrix, hEffect, hAttributes, GpUnit.UnitPixel);

                // Everything worked, return true
                return(true);
            } catch (Exception ex) {
                LOG.Error("Problem using GdipDrawImageFX: ", ex);
                return(false);
            } finally {
                if (hEffect != null)
                {
                    // Delete the effect
                    GdipDeleteEffect(hEffect);
                }
                if (hBlurParams != IntPtr.Zero)
                {
                    // Free the memory
                    Marshal.FreeHGlobal(hBlurParams);
                }
            }
        }
コード例 #3
0
ファイル: GDIplus.cs プロジェクト: Maximus325/ShareX
        /// <summary>
        /// Draw the image on the graphics with GDI+ blur effect
        /// </summary>
        /// <returns>false if there is no GDI+ available or an exception occured</returns>
        public static bool DrawWithBlur(Graphics graphics, Bitmap image, Rectangle source, Matrix transform, ImageAttributes imageAttributes, int radius, bool expandEdges)
        {
            if (!IsBlurPossible(radius))
            {
                return false;
            }

            IntPtr hBlurParams = IntPtr.Zero;
            IntPtr hEffect = IntPtr.Zero;

            try
            {
                // Create a BlurParams struct and set the values
                BlurParams blurParams = new BlurParams();
                blurParams.Radius = radius;
                //blurParams.Padding = radius;
                blurParams.ExpandEdges = false;

                // Allocate space in unmanaged memory
                hBlurParams = Marshal.AllocHGlobal(Marshal.SizeOf(blurParams));
                // Copy the structure to the unmanaged memory
                Marshal.StructureToPtr(blurParams, hBlurParams, true);

                // Create the GDI+ BlurEffect, using the Guid
                int status = GdipCreateEffect(BlurEffectGuid, out hEffect);

                // Set the blurParams to the effect
                GdipSetEffectParameters(hEffect, hBlurParams, (uint)Marshal.SizeOf(blurParams));

                // Somewhere it said we can use destinationBitmap.GetHbitmap(), this doesn't work!!
                // Get the private nativeImage property from the Bitmap
                IntPtr hBitmap = GetNativeImage(image);
                IntPtr hGraphics = GetNativeGraphics(graphics);
                IntPtr hMatrix = GetNativeMatrix(transform);
                IntPtr hAttributes = GetNativeImageAttributes(imageAttributes);

                // Create a RECT from the Rectangle
                RECTF sourceRECF = new RECTF(source);
                // Apply the effect to the bitmap in the specified area
                GdipDrawImageFX(hGraphics, hBitmap, ref sourceRECF, hMatrix, hEffect, hAttributes, GpUnit.UnitPixel);

                // Everything worked, return true
                return true;
            }
            catch (Exception ex)
            {
                isBlurEnabled = false;
                LOG.Error("Problem using GdipDrawImageFX: ", ex);
                return false;
            }
            finally
            {
                try
                {
                    if (hEffect != IntPtr.Zero)
                    {
                        // Delete the effect
                        GdipDeleteEffect(hEffect);
                    }
                    if (hBlurParams != IntPtr.Zero)
                    {
                        // Free the memory
                        Marshal.FreeHGlobal(hBlurParams);
                    }
                }
                catch (Exception ex)
                {
                    isBlurEnabled = false;
                    LOG.Error("Problem cleaning up DrawWithBlur: ", ex);
                }
            }
        }
コード例 #4
0
ファイル: GDIplus.cs プロジェクト: Maximus325/ShareX
        /// <summary>
        /// Use the GDI+ blur effect on the bitmap
        /// </summary>
        /// <param name="destinationBitmap">Bitmap to apply the effect to</param>
        /// <param name="area">Rectangle to apply the blur effect to</param>
        /// <param name="radius">0-255</param>
        /// <param name="expandEdges">bool true if the edges are expanded with the radius</param>
        /// <returns>false if there is no GDI+ available or an exception occured</returns>
        public static bool ApplyBlur(Bitmap destinationBitmap, Rectangle area, int radius, bool expandEdges)
        {
            if (!IsBlurPossible(radius))
            {
                return false;
            }
            IntPtr hBlurParams = IntPtr.Zero;
            IntPtr hEffect = IntPtr.Zero;

            try
            {
                // Create a BlurParams struct and set the values
                BlurParams blurParams = new BlurParams();
                blurParams.Radius = radius;
                blurParams.ExpandEdges = expandEdges;

                // Allocate space in unmanaged memory
                hBlurParams = Marshal.AllocHGlobal(Marshal.SizeOf(blurParams));
                // Copy the structure to the unmanaged memory
                Marshal.StructureToPtr(blurParams, hBlurParams, false);

                // Create the GDI+ BlurEffect, using the Guid
                int status = GdipCreateEffect(BlurEffectGuid, out hEffect);

                // Set the blurParams to the effect
                GdipSetEffectParameters(hEffect, hBlurParams, (uint)Marshal.SizeOf(blurParams));

                // Somewhere it said we can use destinationBitmap.GetHbitmap(), this doesn't work!!
                // Get the private nativeImage property from the Bitmap
                IntPtr hBitmap = GetNativeImage(destinationBitmap);

                // Create a RECT from the Rectangle
                RECT rec = new RECT(area);
                // Apply the effect to the bitmap in the specified area
                GdipBitmapApplyEffect(hBitmap, hEffect, ref rec, false, IntPtr.Zero, 0);

                // Everything worked, return true
                return true;
            }
            catch (Exception ex)
            {
                isBlurEnabled = false;
                LOG.Error("Problem using GdipBitmapApplyEffect: ", ex);
                return false;
            }
            finally
            {
                try
                {
                    if (hEffect != IntPtr.Zero)
                    {
                        // Delete the effect
                        GdipDeleteEffect(hEffect);
                    }
                    if (hBlurParams != IntPtr.Zero)
                    {
                        // Free the memory
                        Marshal.FreeHGlobal(hBlurParams);
                    }
                }
                catch (Exception ex)
                {
                    isBlurEnabled = false;
                    LOG.Error("Problem cleaning up ApplyBlur: ", ex);
                }
            }
        }