13,254,187 members (60,850 online)
Rate this:
See more:
I want to convert an image from bitmap to grayscale, but when I load it in a picturebox...not on a button click event...
Like this...I load a bitmap picture and in the picturebox and it appears directly as a grayscale picture...
I found some source code on the Internet, but I don't really kmow how where to put the code to do what I want...
Thank you
Posted 17-Jan-11 12:43pm
Manfred R. Bihy 17-Jan-11 20:11pm

See my modified answer for a code example and start studying MSDN .NET documentation.

Rate this:

## Solution 1

Ahh, google, why hath thou forsaken everyone but me? Google the phrase "c# convert color image to black and white", and this is one of the 202,000 returned results:

```public static Bitmap ConvertBlackAndWhite(Bitmap Image)
{
ColorMatrix TempMatrix = new ColorMatrix();
TempMatrix.Matrix = 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}
};
return TempMatrix.Apply(Image);
}```
Manfred R. Bihy 17-Jan-11 17:54pm

I can tell from the matrix that it is indeed one that creates gray scaled images. Why would they call an gray scale image black and white :=D.
Rate this:

## Solution 2

You'll find this tutorial helpful as it shows three different ways to do what you want. The last is my favorite and does its trick by using a ColorMatrix: http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale[^]!

Hope this helps you!

Modification
Code sample: All you need to create is a form with a PictureBox on it!

```using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GrayImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

//Load an image frim you local file system
Image normalImage = Image.FromFile(@"D:\Data\Media\Photo\2009\25-12-2009\DSC_0073.jpg");
// Turn image into gray scale image
Image grayScaled = (Image)MakeGrayscale3(new Bitmap(normalImage));

pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
// Assign image to picture box
pictureBox1.BackgroundImage = grayScaled;
}

// This is the method form the Web site I told you about
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;
}
}
}```

Best Regards,
Manfred
v2
Manfred R. Bihy 17-Jan-11 19:24pm

Thank you very much for the help, I found myself that tutorial earlyer, but I have another question...where do I implement this code...in the load button?...or in the picturebox?...because I want to display the image already converted
Thank you
bosco_boom 17-Jan-11 20:41pm

Thank you for the code...now I just have to Load the picture from a button...does this changes the code a lot?
bosco_boom 17-Jan-11 20:46pm

I tried to put that method in my code earlier but I got this errors:
"The type or namespace name 'ColorMatrix' could not be found (are you missing a using directive or an assembly reference?)"
"The type or namespace name 'ImageAttributes' could not be found (are you missing a using directive or an assembly reference?)"
Manfred R. Bihy 17-Jan-11 20:53pm

I can clearly see from you comment that you are a beginner so I'll not be too harsh, but you must do some learning on your own. We try to be helpful when somebody has a problem, but you have to show some effort yourself. There are loads of tutorials on the net to get you started and if you're totally new to this I dearly recommend that you buy a book and study it.
bosco_boom 17-Jan-11 20:58pm

you helped me a lot anyway...thank you again...I wasn;t trying to get the code already done...but this errors i don't know how to fix them...that's all
Manfred R. Bihy 17-Jan-11 21:11pm

To use classes like ColorMatrix and ImageAttribute without writing down the fully qualified name you declare which namespaces you want to use with the using statement:

using System.Drawing; //Lets me use Bitmap for instance without havin to write System.Drawing.Bitmap
using System.Drawing.Imaging; // Now I can say ColorMatrix instead System.Drawing.Imaging.ColorMatrix

Some libraries can be directly accessed or the project has them alreay "referenced" and other have to be added explicitely in the solution explorer windows under "References".
bosco_boom 17-Jan-11 21:05pm

Manfred R. Bihy 17-Jan-11 21:13pm

Good, keep on going!
Member 1493570 29-Jan-13 3:33am

i am using the same code but it save as 32 bpp i want it in 8bpp is this possible.
gnpc22 21-Jul-14 13:32pm

I'm also having the same problem. Did you managed to get a solution for 8bpp?
gnpc22 23-Jul-14 4:23am

Just found out it's a limitation of the Image.Save() method. So I changed to saving the image with the FreeImage library.

Top Experts
Last 24hrsThis month
 Richard Deeming 160 GobblesGobbles 144 ppolymorphe 140 Karthik Bangalore 120 Dave Kreskowiak 110
 OriginalGriff 3,934 Karthik Bangalore 2,271 ppolymorphe 1,669 Dave Kreskowiak 1,391 CPallini 1,240