static public Dot9BitmapD2D CreateDot9BoxShadowBitmap(D2DGraphics g, float cornersRadius, float shadowRadius, System.Drawing.Color shadowColor) { int width = (int)(cornersRadius * 2 + shadowRadius * 4 + 2); int height = (int)(cornersRadius * 2 + shadowRadius * 4 + 2); SharpDX.Direct2D1.PixelFormat format = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied); BitmapProperties prop = new BitmapProperties(format); SharpDX.Direct2D1.Bitmap bitmap = new SharpDX.Direct2D1.Bitmap(g.RenderTarget, new Size2(width, height), prop); unsafe { System.Drawing.Bitmap gdibmp = new System.Drawing.Bitmap(width, height); System.Drawing.RectangleF rect = new System.Drawing.RectangleF(shadowRadius, shadowRadius, width - shadowRadius * 2, height - shadowRadius * 2); Graphics gdiGraph = Graphics.FromImage(gdibmp); GraphicsPath myPath = DrawUtils.CreateRoundedRectanglePath(rect, cornersRadius); System.Drawing.Brush brush = new SolidBrush(shadowColor); gdiGraph.FillPath(brush, myPath); brush.Dispose(); GaussianBlur gs = new GaussianBlur((int)shadowRadius); gdibmp = gs.ProcessImage(gdibmp); System.Drawing.Rectangle bitmapRect = new System.Drawing.Rectangle(0, 0, gdibmp.Width, gdibmp.Height); BitmapData srcBmData = gdibmp.LockBits(bitmapRect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); byte * ptr = (byte *)srcBmData.Scan0; bitmap.CopyFromMemory((IntPtr)ptr, srcBmData.Stride); gdibmp.UnlockBits(srcBmData); // gdibmp.Save("f:\\shadow.png"); gdibmp.Dispose(); } int[] widthPts = new int[] { (int)(cornersRadius + shadowRadius * 2 + 1), (int)(width - cornersRadius - shadowRadius * 2 - 1) }; int[] heightPts = new int[] { (int)(cornersRadius + shadowRadius * 2 + 1), (int)(width - cornersRadius - shadowRadius * 2 - 1) }; Dot9BitmapD2D dot9Bitmap = new Dot9BitmapD2D(bitmap, widthPts, heightPts); return(dot9Bitmap); }
static public void BoxShadow(Bitmap processBitmap, int shadowRadius, int[] cornersType, int[] cornersRadius) { int r = shadowRadius; int sideWidth = shadowRadius; int offset = 2; Graphics g; Bitmap bitmap; Rectangle rc; Rectangle rc1; Bitmap effectBitmap; GraphicsPath path; GaussianBlur gs = new GaussianBlur(r); int width = processBitmap.Width; int height = processBitmap.Height; Graphics baseg = Graphics.FromImage(processBitmap); // int leftTopWidth = sideWidth + r; int leftTopHeight = sideWidth + r; bitmap = new Bitmap(leftTopWidth, leftTopHeight); g = Graphics.FromImage(bitmap); rc = new Rectangle(sideWidth, sideWidth, r, r); if (cornersType[0] == 0) { g.FillRectangle(Brushes.Black, rc); effectBitmap = gs.ProcessImage(bitmap); baseg.DrawImage(effectBitmap, 0, 0, new Rectangle(0, 0, sideWidth, sideWidth), GraphicsUnit.Pixel); } else { g.SmoothingMode = SmoothingMode.HighQuality; path = DrawUtils.CreateRoundedRectanglePath(rc, cornersRadius[0]); g.FillPath(Brushes.Black, path); path.Dispose(); effectBitmap = gs.ProcessImage(bitmap); baseg.DrawImage(effectBitmap, 0, 0, new Rectangle(0, 0, sideWidth, sideWidth), GraphicsUnit.Pixel); baseg.DrawImage(effectBitmap, sideWidth, sideWidth, new Rectangle(sideWidth, sideWidth, r, r), GraphicsUnit.Pixel); } g.Dispose(); bitmap.Dispose(); //// int rightTopWidth = sideWidth + r; int rightTopHeight = sideWidth + r; bitmap = new Bitmap(rightTopWidth, rightTopHeight); g = Graphics.FromImage(bitmap); rc = new Rectangle(0, sideWidth, r, r); if (cornersType[0] == 0) { g.FillRectangle(Brushes.Black, rc); effectBitmap = gs.ProcessImage(bitmap); baseg.DrawImage(effectBitmap, width - sideWidth, 0, new Rectangle(rightTopWidth - sideWidth, 0, sideWidth, sideWidth), GraphicsUnit.Pixel); } else { g.SmoothingMode = SmoothingMode.HighQuality; path = DrawUtils.CreateRoundedRectanglePath(rc, cornersRadius[0]); g.FillPath(Brushes.Black, path); path.Dispose(); effectBitmap = gs.ProcessImage(bitmap); baseg.DrawImage(effectBitmap, width - sideWidth, 0, new Rectangle(rightTopWidth - sideWidth, 0, sideWidth, sideWidth), GraphicsUnit.Pixel); baseg.DrawImage(effectBitmap, width - sideWidth - r, sideWidth, new Rectangle(0, sideWidth, r, r), GraphicsUnit.Pixel); } g.Dispose(); bitmap.Dispose(); //// int leftBottomWidth = sideWidth + r; int leftBottomHeight = sideWidth + r; bitmap = new Bitmap(leftBottomWidth, leftBottomHeight); g = Graphics.FromImage(bitmap); rc = new Rectangle(sideWidth, 0, r, r); if (cornersType[2] == 0) { g.FillRectangle(Brushes.Black, rc); effectBitmap = gs.ProcessImage(bitmap); baseg.DrawImage(effectBitmap, 0, height - sideWidth, new Rectangle(0, leftBottomHeight - sideWidth, sideWidth, sideWidth), GraphicsUnit.Pixel); } else { g.SmoothingMode = SmoothingMode.HighQuality; path = DrawUtils.CreateRoundedRectanglePath(rc, cornersRadius[0]); g.FillPath(Brushes.Black, path); path.Dispose(); effectBitmap = gs.ProcessImage(bitmap); baseg.DrawImage(effectBitmap, 0, height - sideWidth, new Rectangle(0, leftBottomHeight - sideWidth, sideWidth, sideWidth), GraphicsUnit.Pixel); baseg.DrawImage(effectBitmap, sideWidth, height - sideWidth - r, new Rectangle(sideWidth, 0, r, r), GraphicsUnit.Pixel); } g.Dispose(); bitmap.Dispose(); //// int rightBottomWidth = sideWidth + r; int rightBottomHeight = sideWidth + r; bitmap = new Bitmap(rightBottomWidth, rightBottomHeight); g = Graphics.FromImage(bitmap); rc = new Rectangle(0, 0, r, r); if (cornersType[3] == 0) { g.FillRectangle(Brushes.Black, rc); effectBitmap = gs.ProcessImage(bitmap); baseg.DrawImage(effectBitmap, width - sideWidth, height - sideWidth, new Rectangle(rightBottomHeight - sideWidth, rightBottomHeight - sideWidth, sideWidth, sideWidth), GraphicsUnit.Pixel); } else { g.SmoothingMode = SmoothingMode.HighQuality; path = DrawUtils.CreateRoundedRectanglePath(rc, cornersRadius[0]); g.FillPath(Brushes.Black, path); path.Dispose(); effectBitmap = gs.ProcessImage(bitmap); baseg.DrawImage(effectBitmap, width - sideWidth, height - sideWidth, new Rectangle(rightBottomHeight - sideWidth, rightBottomHeight - sideWidth, sideWidth, sideWidth), GraphicsUnit.Pixel); baseg.DrawImage(effectBitmap, width - sideWidth - r, height - sideWidth - r, new Rectangle(0, 0, r, r), GraphicsUnit.Pixel); } g.Dispose(); bitmap.Dispose(); //// int topWidth = width; int topHeight = r + sideWidth; bitmap = new Bitmap(topWidth, topHeight); g = Graphics.FromImage(bitmap); rc = new Rectangle(sideWidth, sideWidth, width - sideWidth * 2, r); if (cornersType[0] == 1) { g.SmoothingMode = SmoothingMode.HighQuality; rc1 = new Rectangle(rc.X, rc.Y, rc.Width / 2 + offset, rc.Height); path = DrawUtils.CreateRoundedRectanglePath(rc1, cornersRadius[0]); g.FillPath(Brushes.Black, path); } else { rc1 = new Rectangle(rc.X, rc.Y, rc.Width / 2 + offset, rc.Height); g.FillRectangle(Brushes.Black, rc1); } if (cornersType[1] == 1) { g.SmoothingMode = SmoothingMode.HighQuality; rc1 = new Rectangle(rc.Right - rc.Width / 2 - offset, rc.Y, rc.Width / 2 + offset, rc.Height); path = DrawUtils.CreateRoundedRectanglePath(rc1, cornersRadius[1]); g.FillPath(Brushes.Black, path); } else { rc1 = new Rectangle(rc.Right - rc.Width / 2 - offset, rc.Y, rc.Width / 2 + offset, rc.Height); g.FillRectangle(Brushes.Black, rc1); } effectBitmap = gs.ProcessImage(bitmap); baseg.DrawImage(effectBitmap, sideWidth, 0, new Rectangle(sideWidth, 0, topWidth - sideWidth * 2, sideWidth), GraphicsUnit.Pixel); g.Dispose(); bitmap.Dispose(); //// int bottomWidth = width; int bottomHeight = r + sideWidth; bitmap = new Bitmap(bottomWidth, bottomHeight); g = Graphics.FromImage(bitmap); rc = new Rectangle(sideWidth, 0, bottomWidth - 2 * sideWidth, r); if (cornersType[2] == 1) { g.SmoothingMode = SmoothingMode.HighQuality; rc1 = new Rectangle(rc.X, rc.Y, rc.Width / 2 + offset, rc.Height); path = DrawUtils.CreateRoundedRectanglePath(rc1, cornersRadius[0]); g.FillPath(Brushes.Black, path); } else { rc1 = new Rectangle(rc.X, rc.Y, rc.Width / 2 + offset, rc.Height); g.FillRectangle(Brushes.Black, rc1); } if (cornersType[3] == 1) { g.SmoothingMode = SmoothingMode.HighQuality; rc1 = new Rectangle(rc.Right - rc.Width / 2 - offset, rc.Y, rc.Width / 2 + offset, rc.Height); path = DrawUtils.CreateRoundedRectanglePath(rc1, cornersRadius[1]); g.FillPath(Brushes.Black, path); } else { rc1 = new Rectangle(rc.Right - rc.Width / 2 - offset, rc.Y, rc.Width / 2 + offset, rc.Height); g.FillRectangle(Brushes.Black, rc1); } effectBitmap = gs.ProcessImage(bitmap); baseg.DrawImage(effectBitmap, sideWidth, height - sideWidth, new Rectangle(sideWidth, bottomHeight - sideWidth, bottomWidth - sideWidth * 2, sideWidth), GraphicsUnit.Pixel); g.Dispose(); bitmap.Dispose(); //// int leftWidth = sideWidth + r; int leftHeight = height; bitmap = new Bitmap(leftWidth, leftHeight); g = Graphics.FromImage(bitmap); rc = new Rectangle(sideWidth, sideWidth, r, leftHeight - sideWidth * 2); if (cornersType[0] == 1) { g.SmoothingMode = SmoothingMode.HighQuality; rc1 = new Rectangle(rc.X, rc.Y, rc.Width, rc.Height / 2 + offset); path = DrawUtils.CreateRoundedRectanglePath(rc1, cornersRadius[0]); g.FillPath(Brushes.Black, path); } else { rc1 = new Rectangle(rc.X, rc.Y, rc.Width, rc.Height / 2 + offset); g.FillRectangle(Brushes.Black, rc1); } if (cornersType[2] == 1) { g.SmoothingMode = SmoothingMode.HighQuality; rc1 = new Rectangle(rc.X, rc.Bottom - rc.Height / 2 - offset, rc.Width, rc.Height / 2 + offset); path = DrawUtils.CreateRoundedRectanglePath(rc1, cornersRadius[2]); g.FillPath(Brushes.Black, path); } else { rc1 = new Rectangle(rc.X, rc.Bottom - rc.Height / 2 - offset, rc.Width, rc.Height / 2 + offset); g.FillRectangle(Brushes.Black, rc1); } effectBitmap = gs.ProcessImage(bitmap); baseg.DrawImage(effectBitmap, 0, sideWidth, new Rectangle(0, sideWidth, sideWidth, leftHeight - sideWidth * 2), GraphicsUnit.Pixel); g.Dispose(); bitmap.Dispose(); //// int rightWidth = sideWidth + r; int rightHeight = height; bitmap = new Bitmap(leftWidth, leftHeight); g = Graphics.FromImage(bitmap); rc = new Rectangle(0, sideWidth, r, rightHeight - sideWidth * 2); if (cornersType[1] == 1) { g.SmoothingMode = SmoothingMode.HighQuality; rc1 = new Rectangle(rc.X, rc.Y, rc.Width, rc.Height / 2 + offset); path = DrawUtils.CreateRoundedRectanglePath(rc1, cornersRadius[1]); g.FillPath(Brushes.Black, path); } else { rc1 = new Rectangle(rc.X, rc.Y, rc.Width, rc.Height / 2 + offset); g.FillRectangle(Brushes.Black, rc1); } if (cornersType[3] == 1) { g.SmoothingMode = SmoothingMode.HighQuality; rc1 = new Rectangle(rc.X, rc.Bottom - rc.Height / 2 - offset, rc.Width, rc.Height / 2 + offset); path = DrawUtils.CreateRoundedRectanglePath(rc1, cornersRadius[3]); g.FillPath(Brushes.Black, path); } else { rc1 = new Rectangle(rc.X, rc.Bottom - rc.Height / 2 - offset, rc.Width, rc.Height / 2 + offset); g.FillRectangle(Brushes.Black, rc1); } effectBitmap = gs.ProcessImage(bitmap); baseg.DrawImage(effectBitmap, width - sideWidth, sideWidth, new Rectangle(leftWidth - sideWidth, sideWidth, sideWidth, leftHeight - sideWidth * 2), GraphicsUnit.Pixel); g.Dispose(); bitmap.Dispose(); baseg.Dispose(); }