/// <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); } } }
/// <summary> /// Returns a RectangleF for a GDI Plus rectangle. /// </summary> /// <param name="rect">The GDI Plus rectangle to get the RectangleF for.</param> /// <returns>A System.Drawing.RectangleF structure.</returns> public static RectangleF ToRectangle(RECTF rect) { return(rect.ToRectangle()); }
private static extern int GdipDrawImageFX(IntPtr graphics, IntPtr bitmap, ref RECTF source, IntPtr matrix, IntPtr effect, IntPtr imageAttributes, GpUnit srcUnit);
/// <summary> /// Returns a RectangleF for a GDI Plus rectangle. /// </summary> /// <param name="rect">The GDI Plus rectangle to get the RectangleF for.</param> /// <returns>A System.Drawing.RectangleF structure.</returns> public static RectangleF ToRectangle(RECTF rect) { return rect.ToRectangle(); }
/// <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); } } }