public override void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode) { int blurRadius = GetFieldValueAsInt(FieldType.BLUR_RADIUS); Rectangle applyRect = ImageHelper.CreateIntersectRectangle(applyBitmap.Size, rect, Invert); if (applyRect.Width == 0 || applyRect.Height == 0) { return; } GraphicsState state = graphics.Save(); if (Invert) { graphics.SetClip(applyRect); graphics.ExcludeClip(rect); } if (GDIplus.IsBlurPossible(blurRadius)) { GDIplus.DrawWithBlur(graphics, applyBitmap, applyRect, null, null, blurRadius, false); } else { using (IFastBitmap fastBitmap = FastBitmap.CreateCloneOf(applyBitmap, applyRect)) { ImageHelper.ApplyBoxBlur(fastBitmap, blurRadius); fastBitmap.DrawTo(graphics, applyRect); } } graphics.Restore(state); }
private void SetInterpolationColorsInternal(ColorBlend blend) { int i1 = (int)blend.Colors.Length; IntPtr j1 = Marshal.AllocHGlobal(4 * i1); IntPtr k = Marshal.AllocHGlobal(4 * i1); int[] nums = new int[i1]; for (int i2 = 0; i2 < i1; i2++) { nums[i2] = blend.Colors[i2].ToArgb(); } Marshal.Copy(nums, 0, j1, i1); Marshal.Copy(blend.Positions, 0, k, i1); int j2 = GDIplus.GdipSetPathGradientPresetBlend(new HandleRef(this, m_nativeBrush), new HandleRef(null, j1), new HandleRef(null, k), i1); Marshal.FreeHGlobal(j1); Marshal.FreeHGlobal(k); if (j2 != 0) { throw new Exception(j2.ToString()); } else { return; } }
public static void Blur(Bitmap sourceImage, int radius) { if (GDIplus.IsBlurPossible(radius)) { GDIplus.ApplyBlur(sourceImage, new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), radius, false); } else { ImageHelper.ApplyBoxBlur(sourceImage, radius); } }
private void CreateBrushInternal() { m_location = m_rect.Location; m_size = m_rect.Size; m_center = new Point(m_location.X + (m_size.Width / 2), m_location.Y + (m_size.Height / 2)); if (m_size.Height < 16 || m_size.Width < 16) { m_factor = 0.25f; } if (m_size.Height < 35 || m_size.Width < 35) { m_factor = 0.5f; } else { m_factor = 1; } //m_gradient *= m_factor; double dX = ((double)m_size.Width / (10f * m_factor)) / 4f; double dY = ((double)m_size.Height / (10f * m_factor)) / 4f; double pX = m_gradient / dX; double pY = m_gradient / dY; double gX = pX / m_gradient; double gY = pY / m_gradient; double gradient = m_gradient / (float)(40f / 2f); double gradW = 4f / ((float)m_size.Width / 2f); double gradH = 4f / ((float)m_size.Height / 2f); double gradX = 1 - gX; double gradY = 1 - gY; float ray = m_rounded / 2; // double alpha = 45 * (Math.PI / 180); // float delta = (float)(ray * Math.Sin(alpha)); // // PointF[] pts = new PointF[17] // { // /* 0 */ new PointF(m_center.X,m_location.Y), // /* 1 */ new PointF(m_rect.Right-ray,m_location.Y), // /* 2 */ new PointF((m_rect.Right-ray)+delta,m_location.Y+(ray-delta)), // /* 3 */ new PointF(m_rect.Right,m_location.Y+ray), // /* 4 */ new PointF(m_rect.Right,m_center.Y), // /* 5 */ new PointF(m_rect.Right,m_rect.Bottom-ray), // /* 6 */ new PointF((m_rect.Right-ray)+delta,m_rect.Bottom-(ray-delta)), // /* 7 */ new PointF(m_rect.Right-ray,m_rect.Bottom), // /* 8 */ new PointF(m_center.X,m_rect.Bottom), // /* 9 */ new PointF(m_location.X+ray,m_rect.Bottom), // /* 10 */ new PointF(m_location.X+(ray-delta),m_rect.Bottom-(ray-delta)), // /* 11 */ new PointF(m_location.X,m_rect.Bottom-ray), // /* 12 */ new PointF(m_location.X,m_center.Y), // /* 13 */ new PointF(m_location.X,m_location.Y+ray), // /* 14 */ new PointF(m_location.X+(ray-delta),m_location.Y+(ray-delta)), // /* 15 */ new PointF(m_location.X+ray,m_location.Y), // /* 16 */ new PointF(m_center.X,m_location.Y) // }; ////***************** NON UTILIZZATE A CAUSA DELLE API *******************//// //GraphicsPath gp = new GraphicsPath(); //gp.AddCurve(pts, 0, 16, 0.05F); ////**********************************************************************//// m_gp = new GraphicsPath(); //m_gp.AddCurve(pts, 0, 16, 0.05F); m_gp.AddLine(new PointF(m_center.X, m_location.Y), new PointF(m_rect.Right - ray, m_location.Y)); m_gp.AddArc(new RectangleF(m_rect.Right - m_rounded, m_location.Y, m_rounded, m_rounded), 270, 90); m_gp.AddLine(new PointF(m_rect.Right, m_location.Y + ray), new PointF(m_rect.Right, m_rect.Bottom - ray)); m_gp.AddArc(new RectangleF(m_rect.Right - m_rounded, m_rect.Bottom - m_rounded, m_rounded, m_rounded), 0, 90); m_gp.AddLine(new PointF(m_rect.Right - ray, m_rect.Bottom), new PointF(m_location.X + ray, m_rect.Bottom)); m_gp.AddArc(new RectangleF(m_location.X, m_rect.Bottom - m_rounded, m_rounded, m_rounded), 90, 90); m_gp.AddLine(new PointF(m_location.X, m_rect.Bottom - ray), new PointF(m_location.X, m_location.Y + ray)); m_gp.AddArc(new RectangleF(m_location.X, m_location.Y, m_rounded, m_rounded), 180, 90); m_gp.AddLine(new PointF(m_location.X + ray, m_location.Y), new PointF(m_center.X, m_location.Y)); m_gp.CloseFigure(); //// ******** PROVA ********** //// IntPtr nativePath = IntPtr.Zero; IntPtr ptrPoints = GDIplus.ConvertPointToMemory(m_gp.PathPoints); int iRet = GDIplus.GdipCreatePath((int)FillMode.Alternate, ref nativePath); HandleRef pathRef = new HandleRef(null, nativePath); iRet = GDIplus.GdipAddPathLine(pathRef, m_center.X, m_location.Y, m_rect.Right - ray, m_location.Y); iRet = GDIplus.GdipAddPathArc(pathRef, m_rect.Right - m_rounded, m_location.Y, m_rounded, m_rounded, 270, 90); iRet = GDIplus.GdipAddPathLine(pathRef, m_rect.Right, m_location.Y + ray, m_rect.Right, m_rect.Bottom - ray); iRet = GDIplus.GdipAddPathArc(pathRef, m_rect.Right - m_rounded, m_rect.Bottom - m_rounded, m_rounded, m_rounded, 0, 90); iRet = GDIplus.GdipAddPathLine(pathRef, m_rect.Right - ray, m_rect.Bottom, m_location.X + ray, m_rect.Bottom); iRet = GDIplus.GdipAddPathArc(pathRef, m_location.X, m_rect.Bottom - m_rounded, m_rounded, m_rounded, 90, 90); iRet = GDIplus.GdipAddPathLine(pathRef, m_location.X, m_rect.Bottom - ray, m_location.X, m_location.Y + ray); iRet = GDIplus.GdipAddPathArc(pathRef, m_location.X, m_location.Y, m_rounded, m_rounded, 180, 90); iRet = GDIplus.GdipAddPathLine(pathRef, m_location.X + ray, m_location.Y, m_center.X, m_location.Y); iRet = GDIplus.GdipClosePathFigure(pathRef); //// ************************* //// ColorBlend cblend = new ColorBlend(); float[] positions = new float[] { 0.0F, (float)gradient, 1F }; Color[] colors = new Color[] { m_color1, m_color2, m_color3 }; cblend.Positions = positions; cblend.Colors = colors; ////***************** NON UTILIZZATE A CAUSA DELLE API *******************//// //PathGradientBrush pgb = new PathGradientBrush(m_gp.PathPoints); //pgb.CenterPoint = m_center; //pgb.CenterColor = m_centercolor; //pgb.InterpolationColors = cblend; //pgb.FocusScales = new PointF((float)gradX, (float)gradY); ////**********************************************************************//// // crea il PathGradientBrush m_nativeBrush = IntPtr.Zero; //IntPtr j = GDIplus.ConvertPointToMemory(m_gp.PathPoints);//pts // /*int*/ iRet = GDIplus.GdipCreatePathGradient(new HandleRef(null, j), (int)pts.Length, (int)WrapMode.Clamp, out m_nativeBrush); iRet = GDIplus.GdipCreatePathGradientFromPath(pathRef, out m_nativeBrush); HandleRef brushRef = new HandleRef(null, m_nativeBrush); // imposta il CenterPoint iRet = GDIplus.GdipSetPathGradientCenterPoint(brushRef, new GPPOINTF(m_center)); // imposta InterpolationColors this.SetInterpolationColorsInternal(cblend); // imposta FocusScales iRet = GDIplus.GdipSetPathGradientFocusScales(brushRef, (float)gradX, (float)gradY); base.SetNativeBrush(m_nativeBrush); }
// 由指定bitmap创建一个新有带阴影的bitmap protected Bitmap CreateShadow(Image sourceBitmap, float darkness, int shadowSize, Point shadowOffset, out Point offset, PixelFormat targetPixelformat) { // Create a new "clean" image offset = shadowOffset; offset.X += shadowSize - 1; offset.Y += shadowSize - 1; Bitmap returnImage = ImageOperator.CreateEmptyBitmap(sourceBitmap.Width + (shadowSize * 2), sourceBitmap.Height + (shadowSize * 2), targetPixelformat, Color.Empty, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); // Make sure the shadow is odd, there is no reason for an even blur! if ((shadowSize & 1) == 0) { shadowSize++; } bool useGDIBlur = GDIplus.IsBlurPossible(shadowSize); // Create "mask" for the shadow ColorMatrix maskMatrix = new ColorMatrix(); maskMatrix.Matrix00 = 0; maskMatrix.Matrix11 = 0; maskMatrix.Matrix22 = 0; if (useGDIBlur) { maskMatrix.Matrix33 = darkness + 0.1f; } else { maskMatrix.Matrix33 = darkness; } Rectangle shadowRectangle = new Rectangle(new Point(shadowSize, shadowSize), sourceBitmap.Size); ImageOperator.ApplyColorMatrix((Bitmap)sourceBitmap, Rectangle.Empty, returnImage, shadowRectangle, maskMatrix); // blur "shadow", apply to whole new image if (useGDIBlur) { // Use GDI Blur Rectangle newImageRectangle = new Rectangle(0, 0, returnImage.Width, returnImage.Height); if (!GDIplus.ApplyBlur(returnImage, newImageRectangle, shadowSize + 1, false)) { log.Error("Failed to ApplyBlur in GDIplus."); } } else { // try normal software blur ApplyBoxBlur(returnImage, shadowSize); } // Draw the original image over the shadow using (Graphics graphics = Graphics.FromImage(returnImage)) { // Make sure we draw with the best quality! graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; // draw original with a TextureBrush so we have nice antialiasing! using (Brush textureBrush = new TextureBrush(sourceBitmap, WrapMode.Clamp)) { // We need to do a translate-tranform otherwise the image is wrapped graphics.TranslateTransform(offset.X, offset.Y); graphics.FillRectangle(textureBrush, 0, 0, sourceBitmap.Width, sourceBitmap.Height); } } return(returnImage); }