public static Bitmap AddReflection(Image img, int percentage, int maxAlpha, int minAlpha) { percentage = percentage.Between(1, 100); maxAlpha = maxAlpha.Between(0, 255); minAlpha = minAlpha.Between(0, 255); Bitmap reflection; using (Bitmap bitmapRotate = (Bitmap)img.Clone()) { bitmapRotate.RotateFlip(RotateFlipType.RotateNoneFlipY); reflection = bitmapRotate.Clone(new Rectangle(0, 0, bitmapRotate.Width, (int)(bitmapRotate.Height * ((float)percentage / 100))), PixelFormat.Format32bppArgb); } using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(reflection, true)) { int alphaAdd = maxAlpha - minAlpha; float reflectionHeight = reflection.Height - 1; for (int y = 0; y < reflection.Height; ++y) { for (int x = 0; x < reflection.Width; ++x) { ColorBgra color = unsafeBitmap.GetPixel(x, y); byte alpha = (byte)(maxAlpha - (alphaAdd * (y / reflectionHeight))); if (color.Alpha > alpha) { color.Alpha = alpha; unsafeBitmap.SetPixel(x, y, color); } } } } return(reflection); }
public static Image Apply(this ConvolutionMatrix matrix, Image img) { int factor = Math.Max(matrix.Factor, 1); Bitmap result = (Bitmap)img.Clone(); using (UnsafeBitmap source = new UnsafeBitmap((Bitmap)img, true, ImageLockMode.ReadOnly)) using (UnsafeBitmap dest = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly)) { int height = source.Height - 2; int width = source.Width - 2; ColorBgra[,] pixelColor = new ColorBgra[3, 3]; int pixel; ColorBgra color = new ColorBgra(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { pixelColor[0, 0] = source.GetPixel(x, y); pixelColor[0, 1] = source.GetPixel(x, y + 1); pixelColor[0, 2] = source.GetPixel(x, y + 2); pixelColor[1, 0] = source.GetPixel(x + 1, y); pixelColor[1, 1] = source.GetPixel(x + 1, y + 1); pixelColor[1, 2] = source.GetPixel(x + 1, y + 2); pixelColor[2, 0] = source.GetPixel(x + 2, y); pixelColor[2, 1] = source.GetPixel(x + 2, y + 1); pixelColor[2, 2] = source.GetPixel(x + 2, y + 2); pixel = (((pixelColor[0, 0].Blue * matrix.Matrix[0, 0]) + (pixelColor[1, 0].Blue * matrix.Matrix[1, 0]) + (pixelColor[2, 0].Blue * matrix.Matrix[2, 0]) + (pixelColor[0, 1].Blue * matrix.Matrix[0, 1]) + (pixelColor[1, 1].Blue * matrix.Matrix[1, 1]) + (pixelColor[2, 1].Blue * matrix.Matrix[2, 1]) + (pixelColor[0, 2].Blue * matrix.Matrix[0, 2]) + (pixelColor[1, 2].Blue * matrix.Matrix[1, 2]) + (pixelColor[2, 2].Blue * matrix.Matrix[2, 2])) / factor) + matrix.Offset; if (pixel < 0) pixel = 0; else if (pixel > 255) pixel = 255; color.Blue = (byte)pixel; pixel = (((pixelColor[0, 0].Green * matrix.Matrix[0, 0]) + (pixelColor[1, 0].Green * matrix.Matrix[1, 0]) + (pixelColor[2, 0].Green * matrix.Matrix[2, 0]) + (pixelColor[0, 1].Green * matrix.Matrix[0, 1]) + (pixelColor[1, 1].Green * matrix.Matrix[1, 1]) + (pixelColor[2, 1].Green * matrix.Matrix[2, 1]) + (pixelColor[0, 2].Green * matrix.Matrix[0, 2]) + (pixelColor[1, 2].Green * matrix.Matrix[1, 2]) + (pixelColor[2, 2].Green * matrix.Matrix[2, 2])) / factor) + matrix.Offset; if (pixel < 0) pixel = 0; else if (pixel > 255) pixel = 255; color.Green = (byte)pixel; pixel = (((pixelColor[0, 0].Red * matrix.Matrix[0, 0]) + (pixelColor[1, 0].Red * matrix.Matrix[1, 0]) + (pixelColor[2, 0].Red * matrix.Matrix[2, 0]) + (pixelColor[0, 1].Red * matrix.Matrix[0, 1]) + (pixelColor[1, 1].Red * matrix.Matrix[1, 1]) + (pixelColor[2, 1].Red * matrix.Matrix[2, 1]) + (pixelColor[0, 2].Red * matrix.Matrix[0, 2]) + (pixelColor[1, 2].Red * matrix.Matrix[1, 2]) + (pixelColor[2, 2].Red * matrix.Matrix[2, 2])) / factor) + matrix.Offset; if (pixel < 0) pixel = 0; else if (pixel > 255) pixel = 255; color.Red = (byte)pixel; color.Alpha = pixelColor[1, 1].Alpha; dest.SetPixel(x + 1, y + 1, color); } } } return result; }
public static Image Apply(this ConvolutionMatrix matrix, Image img) { int factor = Math.Max(matrix.Factor, 1); Bitmap result = (Bitmap)img.Clone(); using (UnsafeBitmap source = new UnsafeBitmap((Bitmap)img, true, ImageLockMode.ReadOnly)) using (UnsafeBitmap dest = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly)) { int height = source.Height - 2; int width = source.Width - 2; ColorBgra[,] pixelColor = new ColorBgra[3, 3]; int pixel; ColorBgra color = new ColorBgra(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { pixelColor[0, 0] = source.GetPixel(x, y); pixelColor[0, 1] = source.GetPixel(x, y + 1); pixelColor[0, 2] = source.GetPixel(x, y + 2); pixelColor[1, 0] = source.GetPixel(x + 1, y); pixelColor[1, 1] = source.GetPixel(x + 1, y + 1); pixelColor[1, 2] = source.GetPixel(x + 1, y + 2); pixelColor[2, 0] = source.GetPixel(x + 2, y); pixelColor[2, 1] = source.GetPixel(x + 2, y + 1); pixelColor[2, 2] = source.GetPixel(x + 2, y + 2); pixel = (((pixelColor[0, 0].Blue * matrix.Matrix[0, 0]) + (pixelColor[1, 0].Blue * matrix.Matrix[1, 0]) + (pixelColor[2, 0].Blue * matrix.Matrix[2, 0]) + (pixelColor[0, 1].Blue * matrix.Matrix[0, 1]) + (pixelColor[1, 1].Blue * matrix.Matrix[1, 1]) + (pixelColor[2, 1].Blue * matrix.Matrix[2, 1]) + (pixelColor[0, 2].Blue * matrix.Matrix[0, 2]) + (pixelColor[1, 2].Blue * matrix.Matrix[1, 2]) + (pixelColor[2, 2].Blue * matrix.Matrix[2, 2])) / factor) + matrix.Offset; if (pixel < 0) { pixel = 0; } else if (pixel > 255) { pixel = 255; } color.Blue = (byte)pixel; pixel = (((pixelColor[0, 0].Green * matrix.Matrix[0, 0]) + (pixelColor[1, 0].Green * matrix.Matrix[1, 0]) + (pixelColor[2, 0].Green * matrix.Matrix[2, 0]) + (pixelColor[0, 1].Green * matrix.Matrix[0, 1]) + (pixelColor[1, 1].Green * matrix.Matrix[1, 1]) + (pixelColor[2, 1].Green * matrix.Matrix[2, 1]) + (pixelColor[0, 2].Green * matrix.Matrix[0, 2]) + (pixelColor[1, 2].Green * matrix.Matrix[1, 2]) + (pixelColor[2, 2].Green * matrix.Matrix[2, 2])) / factor) + matrix.Offset; if (pixel < 0) { pixel = 0; } else if (pixel > 255) { pixel = 255; } color.Green = (byte)pixel; pixel = (((pixelColor[0, 0].Red * matrix.Matrix[0, 0]) + (pixelColor[1, 0].Red * matrix.Matrix[1, 0]) + (pixelColor[2, 0].Red * matrix.Matrix[2, 0]) + (pixelColor[0, 1].Red * matrix.Matrix[0, 1]) + (pixelColor[1, 1].Red * matrix.Matrix[1, 1]) + (pixelColor[2, 1].Red * matrix.Matrix[2, 1]) + (pixelColor[0, 2].Red * matrix.Matrix[0, 2]) + (pixelColor[1, 2].Red * matrix.Matrix[1, 2]) + (pixelColor[2, 2].Red * matrix.Matrix[2, 2])) / factor) + matrix.Offset; if (pixel < 0) { pixel = 0; } else if (pixel > 255) { pixel = 255; } color.Red = (byte)pixel; color.Alpha = pixelColor[1, 1].Alpha; dest.SetPixel(x + 1, y + 1, color); } } } return(result); }
public static Bitmap AddReflection(Image img, int percentage, int maxAlpha, int minAlpha) { percentage = percentage.Between(1, 100); maxAlpha = maxAlpha.Between(0, 255); minAlpha = minAlpha.Between(0, 255); Bitmap reflection; using (Bitmap bitmapRotate = (Bitmap)img.Clone()) { bitmapRotate.RotateFlip(RotateFlipType.RotateNoneFlipY); reflection = bitmapRotate.Clone(new Rectangle(0, 0, bitmapRotate.Width, (int)(bitmapRotate.Height * ((float)percentage / 100))), PixelFormat.Format32bppArgb); } using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(reflection, true)) { int alphaAdd = maxAlpha - minAlpha; float reflectionHeight = reflection.Height - 1; for (int y = 0; y < reflection.Height; ++y) { for (int x = 0; x < reflection.Width; ++x) { ColorBgra color = unsafeBitmap.GetPixel(x, y); byte alpha = (byte)(maxAlpha - (alphaAdd * (y / reflectionHeight))); if (color.Alpha > alpha) { color.Alpha = alpha; unsafeBitmap.SetPixel(x, y, color); } } } } return reflection; }
private static Bitmap CreateTransparentImage(Bitmap whiteBackground, Bitmap blackBackground) { if (whiteBackground != null && blackBackground != null && whiteBackground.Size == blackBackground.Size) { Bitmap result = new Bitmap(whiteBackground.Width, whiteBackground.Height, PixelFormat.Format32bppArgb); using (UnsafeBitmap whiteBitmap = new UnsafeBitmap(whiteBackground, true, ImageLockMode.ReadOnly)) using (UnsafeBitmap blackBitmap = new UnsafeBitmap(blackBackground, true, ImageLockMode.ReadOnly)) using (UnsafeBitmap resultBitmap = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly)) { int pixelCount = blackBitmap.PixelCount; for (int i = 0; i < pixelCount; i++) { ColorBgra white = whiteBitmap.GetPixel(i); ColorBgra black = blackBitmap.GetPixel(i); double alpha = (black.Red - white.Red + 255) / 255.0; if (alpha == 1) { resultBitmap.SetPixel(i, white); } else if (alpha > 0) { white.Blue = (byte)(black.Blue / alpha); white.Green = (byte)(black.Green / alpha); white.Red = (byte)(black.Red / alpha); white.Alpha = (byte)(255 * alpha); resultBitmap.SetPixel(i, white); } } } return result; } return whiteBackground; }