Вы находитесь на странице: 1из 3

private static Bitmap Blur(Bitmap image, Rectangle rectangle, Int32 blurSize) { Bitmap blurred = new Bitmap(image.Width, image.

Height); // make an exact copy of the bitmap provided using(Graphics graphics = Graphics.FromImage(blurred)) graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height) , new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); // look at every pixel in the blur rectangle for (Int32 xx = rectangle.X; xx < rectangle.X + rectangle.Width; xx++) { for (Int32 yy = rectangle.Y; yy < rectangle.Y + rectangle.Height; yy++) { Int32 avgR = 0, avgG = 0, avgB = 0; Int32 blurPixelCount = 0; // average the color of the red, green and blue for each pixel in th e // blur size while making sure you don't go outside the image bounds for (Int32 x = xx; (x < xx + blurSize && x < image.Width); x++) { for (Int32 y = yy; (y < yy + blurSize && y < image.Height); y++) { Color pixel = blurred.GetPixel(x, y); avgR += pixel.R; avgG += pixel.G; avgB += pixel.B; blurPixelCount++; } } avgR = avgR / blurPixelCount; avgG = avgG / blurPixelCount; avgB = avgB / blurPixelCount; // now that we know the average for the blur size, set each pixel to that color for (Int32 x = xx; x < xx + blurSize && x < image.Width && x < recta ngle.Width; x++) for (Int32 y = yy; y < yy + blurSize && y < image.Height && y < rectangle.Height; y++) blurred.SetPixel(x, y, Color.FromArgb(avgR, avgG, avgB)); } } return blurred; } //////////////////////////////////////////// public static Bitmap Sharpen(Bitmap image) { Bitmap sharpenImage = (Bitmap)image.Clone(); int int int int filterWidth = 3; filterHeight = 3; width = image.Width; height = image.Height;

// Create double[,] filter[0, = filter[2, 0] = filter[1,

sharpening filter. filter = new double[filterWidth, filterHeight]; 0] = filter[0, 1] = filter[0, 2] = filter[1, 0] = filter[1, 2] filter[2, 1] = filter[2, 2] = -1; 1] = 9;

double factor = 1.0; double bias = 0.0; Color[,] result = new Color[image.Width, image.Height]; // Lock image bits for read/write. BitmapData pbits = sharpenImage.LockBits(new Rectangle(0, 0, width, heig ht), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); // Declare an array to hold the bytes of the bitmap. int bytes = pbits.Stride * height; byte[] rgbValues = new byte[bytes]; // Copy the RGB values into the array. System.Runtime.InteropServices.Marshal.Copy(pbits.Scan0, rgbValues, 0, b ytes); int rgb; // Fill the color array with the new sharpened color values. for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { double red = 0.0, green = 0.0, blue = 0.0; for (int filterX = 0; filterX < filterWidth; filterX++) { for (int filterY = 0; filterY < filterHeight; filterY++) { int imageX = (x - filterWidth / 2 + filterX + width) % w idth; int imageY = (y - filterHeight / 2 + filterY + height) % height; rgb = imageY * pbits.Stride + 3 * imageX; red += rgbValues[rgb + 2] * filter[filterX, filterY]; green += rgbValues[rgb + 1] * filter[filterX, filterY]; blue += rgbValues[rgb + 0] * filter[filterX, filterY]; } int r = Math.Min(Math.Max((int)(factor * red + bias), 0), 25 5); int g = Math.Min(Math.Max((int)(factor * green + bias), 0), 255); int b = Math.Min(Math.Max((int)(factor * blue + bias), 0), 2 55); result[x, y] = Color.FromArgb(r, g, b); } } } // Update the image with the sharpened pixels.

for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { rgb = y * pbits.Stride + 3 * x; rgbValues[rgb + 2] = result[x, y].R; rgbValues[rgb + 1] = result[x, y].G; rgbValues[rgb + 0] = result[x, y].B; } } // Copy the RGB values back to the bitmap. System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, pbits.Scan0, b ytes); // Release image bits. sharpenImage.UnlockBits(pbits); return sharpenImage; }

Вам также может понравиться