public static Image CutImageCorner(int topPositionPercentage, int sidePositionPercentage, Image image) { var topPosition = image.Picture.Width * topPositionPercentage / 100; var sidePosition = image.Picture.Width * sidePositionPercentage / 100; var bitmapWithBoundary = (Bitmap)DrawMarkerLine(new Point(topPosition, 0), new Point(0, sidePosition), Color.Red, 1, image).Picture; var currentPoint = new Point(0, 0); while (bitmapWithBoundary.GetPixel(0, currentPoint.Y).ToArgb() != Color.Red.ToArgb()) { while (bitmapWithBoundary.GetPixel(currentPoint.X, currentPoint.Y).ToArgb() != Color.Red.ToArgb()) { bitmapWithBoundary.SetPixel(currentPoint.X, currentPoint.Y, Color.Black); currentPoint.X++; } while (bitmapWithBoundary.GetPixel(currentPoint.X, currentPoint.Y).ToArgb() == Color.Red.ToArgb()) { bitmapWithBoundary.SetPixel(currentPoint.X, currentPoint.Y, Color.Black); currentPoint.X++; } currentPoint.Y++; currentPoint.X = 0; } var sabotagedImage = image.Copy(); sabotagedImage.Picture = bitmapWithBoundary; return sabotagedImage; }
private static Image Blur(int intensity, Image image) { var newImage = new Bitmap(image.Picture); for (int x = 0; x < newImage.Width; x++) { for (int y = 0; y < newImage.Height; y++) { var pixelOffset = intensity/ 10; var currentPixel = newImage.GetPixel(x, y); var prevX = newImage.GetPixel(x - pixelOffset < 0 ? 0 : x - pixelOffset, y); var nextX = newImage.GetPixel(x + pixelOffset >= newImage.Width ? newImage.Width - 1 : x + pixelOffset, y); var prevY = newImage.GetPixel(x, y - pixelOffset < 0 ? 0 : y - pixelOffset); var nextY = newImage.GetPixel(x, y + pixelOffset >= newImage.Height ? newImage.Height - 1 : y + pixelOffset); int avgR = (int) ((prevX.R + prevY.R + nextX.R + nextY.R)/4); avgR += (avgR - currentPixel.R) / (intensity == 0 ? 1 : intensity); int avgG = (int) ((prevX.G + prevY.G + nextX.G + nextY.G)/4); avgG += (avgG - currentPixel.G) / (intensity == 0 ? 1 : intensity); int avgB = (int) ((prevX.B + prevY.B + nextX.B + nextY.B)/4); avgB += (avgB - currentPixel.B) / (intensity == 0 ? 1 : intensity); newImage.SetPixel(x, y, Color.FromArgb(avgR, avgG, avgB)); } } using (MemoryStream memoryStream = new MemoryStream()) { newImage.Save(memoryStream, ImageFormat.Png); newImage = new Bitmap(memoryStream); } var sabotagedImage = image.Copy(); sabotagedImage.Picture = newImage; return sabotagedImage; }
private static Image AdjustBrightness(int brightness, Image image) { var newImage = new Bitmap(image.Picture); Bitmap clonedImage = newImage; float adjustedBrightness = (float)brightness / 255.0f; // create matrix that will brighten and contrast the image float[][] ptsArray ={ new float[] {1, 0, 0, 0, 0}, // scale red new float[] {0, 1, 0, 0, 0}, // scale green new float[] {0, 0, 1, 0, 0}, // scale blue new float[] {0, 0, 0, 1, 0}, new float[] {adjustedBrightness, adjustedBrightness, adjustedBrightness, 1, 1}}; var imageAttributes = new ImageAttributes(); imageAttributes.ClearColorMatrix(); imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); // Copy back to the original image from the cloned image Graphics g = Graphics.FromImage(newImage); g.DrawImage(clonedImage, new Rectangle(0, 0, clonedImage.Width, clonedImage.Height) , 0, 0, clonedImage.Width, clonedImage.Height, GraphicsUnit.Pixel, imageAttributes); g.Flush(); using (MemoryStream memoryStream = new MemoryStream()) { newImage.Save(memoryStream, ImageFormat.Png); newImage = new Bitmap(memoryStream); } var sabotagedImage = image.Copy(); sabotagedImage.Picture = newImage; return sabotagedImage; }
public static Image RotateImage(int angle, Image image) { var bitmap = image.Picture; http://stackoverflow.com/questions/14184700/how-to-rotate-image-x-degrees-in-c angle = angle % 360; if (angle > 180) angle -= 360; var sin = (float)Math.Abs(Math.Sin(angle * Math.PI / 180.0)); // this function takes radians var cos = (float)Math.Abs(Math.Cos(angle * Math.PI / 180.0)); // this one too var newImgWidth = sin * bitmap.Height + cos * bitmap.Width; var newImgHeight = sin * bitmap.Width + cos * bitmap.Height; var originX = 0f; var originY = 0f; if (angle > 0) { if (angle <= 90) originX = sin * bitmap.Height; else { originX = newImgWidth; originY = newImgHeight - sin * bitmap.Width; } } else { if (angle >= -90) originY = sin * bitmap.Width; else { originX = newImgWidth - sin * bitmap.Height; originY = newImgHeight; } } var newImage = new Bitmap((int)newImgWidth, (int)newImgHeight, PixelFormat.Format32bppArgb); var graphics = Graphics.FromImage(newImage); graphics.TranslateTransform(originX, originY); // offset the origin to our calculated values graphics.RotateTransform(angle); // set up rotate graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; graphics.DrawImageUnscaled(bitmap, 0, 0); // draw the image at 0, 0 graphics.Dispose(); using (MemoryStream memoryStream = new MemoryStream()) { newImage.Save(memoryStream, ImageFormat.Png); newImage = new Bitmap(memoryStream); } var sabotagedImage = image.Copy(); sabotagedImage.Picture = newImage; return sabotagedImage; }
public static Image GenerateNoise(int permille, Image image) { var newImage = new Bitmap(image.Picture); Random rng = new Random(); var totalcount = 0; var hitCount = 0; for (int x = 0; x < newImage.Width; x++) { for (int y = 0; y < newImage.Height; y++) { totalcount++; var chance = rng.Next(1, 1000); var color = rng.Next(0, 2); if (color == 1) { color = 255; } else { color = 0; } if (chance <= permille) { hitCount++; newImage.SetPixel(x, y, Color.FromArgb(255, color, color, color)); } } } using (MemoryStream memoryStream = new MemoryStream()) { newImage.Save(memoryStream, ImageFormat.Png); newImage = new Bitmap(memoryStream); } var sabotagedImage = image.Copy(); sabotagedImage.Picture = newImage; return sabotagedImage; }
public static Image DrawMarkerLine(Point start, Point end, Color markerColor, float markerWidth, Image image) { var bitmap = image.Picture; markerWidth = markerWidth*((float) bitmap.Height/100); var newImage = new Bitmap(bitmap); var graphics = Graphics.FromImage(newImage); graphics.DrawLine(new Pen(markerColor, markerWidth), start.X, start.Y, end.X, end.Y); graphics.Dispose(); using (MemoryStream memoryStream = new MemoryStream()) { newImage.Save(memoryStream, ImageFormat.Png); newImage = new Bitmap(memoryStream); } var sabotagedImage = image.Copy(); sabotagedImage.Picture = newImage; return sabotagedImage; }