Created
December 28, 2013 23:37
-
-
Save superic/8165689 to your computer and use it in GitHub Desktop.
Make an image black and white in c#. For more information: http://eric.tumblr.com/post/71459461047/make-an-image-black-and-white-in-c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static Bitmap BlackAndWhite(Bitmap image, Rectangle rectangle) | |
{ | |
Bitmap blackAndWhite = new System.Drawing.Bitmap(image.Width, image.Height); | |
// make an exact copy of the bitmap provided | |
using(Graphics graphics = System.Drawing.Graphics.FromImage(blackAndWhite)) | |
graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, image.Width, image.Height), | |
new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); | |
// for every pixel in the rectangle region | |
for (Int32 xx = rectangle.X; xx < rectangle.X + rectangle.Width && xx < image.Width; xx++) | |
{ | |
for (Int32 yy = rectangle.Y; yy < rectangle.Y + rectangle.Height && yy < image.Height; yy++) | |
{ | |
// average the red, green and blue of the pixel to get a gray value | |
Color pixel = blackAndWhite.GetPixel(xx, yy); | |
Int32 avg = (pixel.R + pixel.G + pixel.B) / 3; | |
blackAndWhite.SetPixel(xx, yy, Color.FromArgb(0, avg, avg, avg)); | |
} | |
} | |
return blackAndWhite; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale | |
public static Bitmap MakeGrayscale3(Bitmap original) | |
{ | |
//create a blank bitmap the same size as original | |
Bitmap newBitmap = new Bitmap(original.Width, original.Height); | |
//get a graphics object from the new image | |
Graphics g = Graphics.FromImage(newBitmap); | |
//create the grayscale ColorMatrix | |
ColorMatrix colorMatrix = new ColorMatrix( | |
new float[][] | |
{ | |
new float[] {.3f, .3f, .3f, 0, 0}, | |
new float[] {.59f, .59f, .59f, 0, 0}, | |
new float[] {.11f, .11f, .11f, 0, 0}, | |
new float[] {0, 0, 0, 1, 0}, | |
new float[] {0, 0, 0, 0, 1} | |
}); | |
//create some image attributes | |
ImageAttributes attributes = new ImageAttributes(); | |
//set the color matrix attribute | |
attributes.SetColorMatrix(colorMatrix); | |
//draw the original image on the new image | |
//using the grayscale color matrix | |
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), | |
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); | |
//dispose the Graphics object | |
g.Dispose(); | |
return newBitmap; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static Bitmap BlackAndWhite(Bitmap image) | |
{ | |
return BlackAndWhite(image, new Rectangle(0, 0, image.Width, image.Height)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment