Convert grayscale image

Is there a way to convert the image to grayscale at 16 bits per pixel, instead of setting the brightness of each of the r, g, and b components. I currently have bmp from a file.

Bitmap c = new Bitmap("filename"); 

I want bitmap d, i.e. a black and white version of c. I see a constructor that includes System.Drawing.Imaging.PixelFormat, but I don’t understand how to use it. I am new to image processing and the corresponding C # libraries, but have average experience with C #.

Any help, link to an online source, tip or suggestion would be appreciated.

EDIT: d - black and white version c.

+51
c # image-processing bitmap grayscale
Feb 15 '10 at 12:37
source share
6 answers

"I need bitmap d, it's shades of gray. I see a constructor that includes System.Drawing.Imaging.PixelFormat, but I don’t understand how to use this."

Here is how to do it

 Bitmap grayScaleBP = new System.Drawing.Bitmap(2, 2, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale); 

EDIT: To Convert to Grayscale

  Bitmap c = new Bitmap("fromFile"); Bitmap d; int x, y; // Loop through the images pixels to reset color. for (x = 0; x < c.Width; x++) { for (y = 0; y < c.Height; y++) { Color pixelColor = c.GetPixel(x, y); Color newColor = Color.FromArgb(pixelColor.R, 0, 0); c.SetPixel(x, y, newColor); // Now greyscale } } d = c; // d is grayscale version of c 

A faster version from switchonthecode follow the link for a complete analysis:

 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; } 
+63
Feb 15 '10 at 12:50
source share
 Bitmap d = new Bitmap(c.Width, c.Height); for (int i = 0; i < c.Width; i++) { for (int x = 0; x < c.Height; x++) { Color oc = c.GetPixel(i, x); int grayScale = (int)((oc.R * 0.3) + (oc.G * 0.59) + (oc.B * 0.11)); Color nc = Color.FromArgb(oc.A, grayScale, grayScale, grayScale); d.SetPixel(i, x, nc); } } 

Thus, it also supports alpha channel.
Enjoy it.

+31
Oct 23 2018-10-23
source share

There is a static method in the ToolStripRenderer class named CreateDisabledImage . Its use is as simple as:

 Bitmap c = new Bitmap("filename"); Image d = ToolStripRenderer.CreateDisabledImage(c); 

It uses a slightly different matrix than the one in the accepted answer, and additionally multiplies it by a transparency of 0.7, so the effect is slightly different from the shade of gray, but if you just want to make the image gray, it is the simplest and best solution.

+14
Sep 29 '15 at 12:21
source share

None of the above examples create 8-bit (8bpp) bitmaps. Some software, such as image processing, only supports 8bpp. Unfortunately, MS.NET libraries do not have a solution. The format of PixelFormat.Format8bppIndexed looks promising, but after many attempts I could not get it to work.

To create a true 8-bit raster file, you need to create the appropriate headers . I eventually found the Grayscale library for creating 8-bit raster (BMP) files. The code is very simple:

 Image image = Image.FromFile("c:/path/to/image.jpg"); GrayBMP_File.CreateGrayBitmapFile(image, "c:/path/to/8bpp/image.bmp"); 

The code for this project is far from good, but it works, with one small problem that is easy to install. The author hard-coded the image resolution to 10x10. Image processing software doesn't like this. The fix is ​​open GrayBMP_File.cs (yes, funky file name, I know) and replace lines 50 and 51 with the code below. In the example, the resolution is set to 200x200, but you must change it to the correct number.

 int resX = 200; int resY = 200; // horizontal resolution Copy_to_Index(DIB_header, BitConverter.GetBytes(resX * 100), 24); // vertical resolution Copy_to_Index(DIB_header, BitConverter.GetBytes(resY * 100), 28); 
+6
Apr 23 2018-11-11T00:
source share

To summarize a few points: There are several pixel options that, while simple, are simply not fast.

@Luis' comment, referencing: (archived) https://web.archive.org/web/20110827032809/http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to -grayscale is excellent.

It uses three different options and includes timings for each.

+6
Aug 16 '11 at 10:22
source share

The following is the simplest solution:

 Bitmap bt = new Bitmap("imageFilePath"); for (int y = 0; y < bt.Height; y++) { for (int x = 0; x < bt.Width; x++) { Color c = bt.GetPixel(x, y); int r = cR; int g = cG; int b = cB; int avg = (r + g + b) / 3; bt.SetPixel(x, y, Color.FromArgb(avg,avg,avg)); } } bt.Save("d:\\out.bmp"); 
+4
Jan 22 '15 at 16:21
source share



All Articles