/// <summary> /// Метод применения фильтра "Только красный" /// </summary> /// <param name="input">Изображение</param> /// <returns>Изображение с фильтром</returns> public static Bitmap Armageddon(Bitmap input) { int width = input.Width; int height = input.Height; var output = new Bitmap(input); var run = new BitmapUnsafeMethods(output); run.LockImage(); for (int y = 0; y < height - 1; y++) { for (int x = 0; x < width - 1; x++) { Color color = run.GetPixel(x, y); byte a = color.A; byte r = color.R; var g = (byte) (color.G*0); var b = (byte) (color.B*0); run.SetPixel(x, y, Color.FromArgb(a, r, g, b)); } } run.UnlockImage(); return output; }
/// <summary> /// Метод для поворота изображения на 90 градусов против часовой стрелки /// </summary> /// <param name="input">Изображение</param> /// <returns>Изображение с фильтром</returns> public static Bitmap Anticlockwise(Bitmap input) { int width = input.Height; int height = input.Width; var output = new Bitmap(width, height); var run = new BitmapUnsafeMethods(output); var toout = new BitmapUnsafeMethods(input); run.LockImage(); toout.LockImage(); for (int y = 0; y <= width - 1; y++) { for (int x = 0; x <= height - 1; x++) { run.SetPixel(y, x, toout.GetPixel(x, y)); } } run.UnlockImage(); toout.UnlockImage(); return output; }
/// <summary> /// Метод применения фильтра "Только синий" /// </summary> /// <param name="input">Изображение</param> /// <returns>Изображение с фильтром</returns> public static Bitmap ChangeColors(Bitmap input) { int width = input.Width; int height = input.Height; var output = new Bitmap(input); Color color; var run = new BitmapUnsafeMethods(output); run.LockImage(); for (int y = 0; y < height - 1; y++) { for (int x = 0; x < width - 1; x++) { color = run.GetPixel(x, y); byte a = color.A; byte r = color.R; byte g = color.G; byte b = color.B; run.SetPixel(x, y, Color.FromArgb(a, g, b, r)); } } run.UnlockImage(); return output; }
/// <summary> /// Метод для горизонтального отражения изображения /// </summary> /// <param name="input">Изображение</param> /// <returns>Изображение с фильтром</returns> public static Bitmap VerticalMap(Bitmap input) { var output = new Bitmap(input); int width = output.Width; int height = output.Height; var run = new BitmapUnsafeMethods(output); run.LockImage(); for (int x = 0; x < width - 1; x++) { int z = 0; for (int y = height - 1; y > z; y--) { Color clr = run.GetPixel(x, y); Color clr2 = run.GetPixel(x, z); run.SetPixel(x, y, clr2); run.SetPixel(x, z, clr); z++; } } run.UnlockImage(); return output; }
/// <summary> /// Метод применения фильтра "Сдвиг" (вертикальный) для изображения /// </summary> /// <param name="input">Изображение</param> /// <returns>Изображение с фильтром</returns> public static Bitmap ShiftVertical(Bitmap input) { var output = new Bitmap(input); int width = output.Width; int height = output.Height; var rand = new Random(); var run = new BitmapUnsafeMethods(output); run.LockImage(); for (int x = 0; x <= width - 1; x++) { if (x%2 != 0) { int ii = rand.Next(1, 5); ii = rand.Next(1, 5); for (int i = 1; i <= ii; i++) { for (int y = height - 1; y > 1; y--) { run.SetPixel(x, y, run.GetPixel(x, y - 1)); } } } else { int ii = rand.Next(1, 5); ii = rand.Next(1, 5); for (int i = 1; i <= ii; i++) { for (int y = 0; y < height - 1; y++) { run.SetPixel(x, y, run.GetPixel(x, y + 1)); } } } } run.UnlockImage(); return output; }
/// <summary> /// Метод применения фильтра "Случайный выбор" для изображения /// </summary> /// <param name="input">Изображение</param> /// <param name="percent">Процент применения</param> /// <returns>Изображение с фильтром</returns> public static Bitmap RandomSelect(Bitmap input, byte percent = 100) { var output = new Bitmap(input); var myrandom = new Random(); int width = input.Width; int height = input.Height; int pixelsCount = width*height; int pixelsPersent = (percent*pixelsCount)/100; var run = new BitmapUnsafeMethods(output); run.LockImage(); for (int i = 1; i <= pixelsPersent; i++) { int pixelToSetColorWidth = myrandom.Next(0, width); int pixelToSetColorHeight = myrandom.Next(0, height); int coordXToGrabColor = myrandom.Next(pixelToSetColorWidth - 5, pixelToSetColorWidth + 1 + 5); int coordYToGrabColor = myrandom.Next(pixelToSetColorHeight - 5, pixelToSetColorHeight + 1 + 5); while ((coordXToGrabColor > pixelToSetColorWidth) || (coordXToGrabColor < 0)) { coordXToGrabColor = myrandom.Next(pixelToSetColorWidth - 5, pixelToSetColorWidth + 1 + 5); } while ((coordYToGrabColor > pixelToSetColorHeight) || (coordYToGrabColor < 0)) { coordYToGrabColor = myrandom.Next(pixelToSetColorHeight - 5, pixelToSetColorHeight + 1 + 5); } Color clr = run.GetPixel(coordXToGrabColor, coordYToGrabColor); run.SetPixel(pixelToSetColorWidth, pixelToSetColorHeight, clr); } run.UnlockImage(); return output; }
public static Bitmap Pixelate(Bitmap input, Int32 pixelateSize = 15) { int width = input.Width; int height = input.Height; var output = new Bitmap(input); var run = new BitmapUnsafeMethods(output); run.LockImage(); for (Int32 xx = 0; xx < width - 1; xx += pixelateSize) { for (Int32 yy = 0; yy < height - 1; yy += pixelateSize) { Int32 offsetX = pixelateSize/2; Int32 offsetY = pixelateSize/2; // make sure that the offset is within the boundry of the image while (xx + offsetX >= width) offsetX--; while (yy + offsetY >= height) offsetY--; // get the pixel color in the center of the soon to be pixelated area Color pixel = run.GetPixel(xx + offsetX, yy + offsetY); // for each pixel in the pixelate size, set it to the center color for (Int32 x = xx; x < xx + pixelateSize && x < width; x++) for (Int32 y = yy; y < yy + pixelateSize && y < height; y++) run.SetPixel(x, y, pixel); } } run.UnlockImage(); return output; }
/// <summary> /// Метод для горизонтального отражения изображения /// </summary> /// <param name="input">Изображение</param> /// <returns>Изображение с фильтром</returns> public static Bitmap Mirroring(Bitmap input) { var output = new Bitmap(input); int width = output.Width; int height = output.Height; var run = new BitmapUnsafeMethods(output); run.LockImage(); for (int y = 0; y < height - 1; y++) { int z = 0; for (int x = width - 1; x > z; x--) { Color clr = run.GetPixel(x, y); Color clr2 = run.GetPixel(z, y); run.SetPixel(x, y, clr2); run.SetPixel(z, y, clr); z++; } } run.UnlockImage(); return output; }
/// <summary> /// Метод применения фильтра "Рассеивание" для изображения /// </summary> /// <param name="input">Изображение</param> /// <param name="percent">Процент применения</param> /// <returns>Изображение с фильтром</returns> public static Bitmap Dispersal(Bitmap input, byte percent = 100) { var output = new Bitmap(input); var myrandom = new Random(); int width = input.Width; int height = input.Height; int pixelsCount = width*height; int pixelsPersent = (percent*pixelsCount)/100; var run = new BitmapUnsafeMethods(output); run.LockImage(); for (int i = 1; i <= pixelsPersent; i++) { int pixel1Width = myrandom.Next(0, width); int pixel1Height = myrandom.Next(0, height); int pixel2Width = myrandom.Next(pixel1Width - 5, pixel1Width + 1 + 5); int pixel2Height = myrandom.Next(pixel1Height - 5, pixel1Height + 1 + 5); while ((pixel2Width > pixel1Width) || (pixel2Width < 0)) { pixel2Width = myrandom.Next(pixel1Width - 5, pixel1Width + 1 + 5); } while ((pixel2Height > pixel1Height) || (pixel2Height < 0)) { pixel2Height = myrandom.Next(pixel1Height - 5, pixel1Height + 1 + 5); } Color clr = run.GetPixel(pixel2Width, pixel2Height); Color clr2 = run.GetPixel(pixel1Width, pixel1Height); run.SetPixel(pixel1Width, pixel1Height, clr); run.SetPixel(pixel2Width, pixel2Height, clr2); } run.UnlockImage(); return output; }