private Bitmap CreateNegative(Image sourceImage) { Bitmap clone = ImageOperator.Clone(sourceImage) as Bitmap; ColorMatrix invertMatrix = new ColorMatrix(new float[][] { new float[] { -1, 0, 0, 0, 0 }, new float[] { 0, -1, 0, 0, 0 }, new float[] { 0, 0, -1, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 1, 1, 1, 1, 1 } }); ImageOperator.ApplyColorMatrix(clone, invertMatrix); return(clone); }
// 由指定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); }