Click here to Skip to main content
13,344,290 members (54,855 online)
Rate this:
Please Sign up or sign in to vote.
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: bad
Please Sign up or sign in to vote.

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: bad
Please Sign up or sign in to vote.

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:[^]!

Hope this helps you!

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()
            //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
            //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
            return newBitmap;

Best Regards,
Manfred R. Bihy 17-Jan-11 19:24pm
Moved from OP's answer:
Thank you very much for the help, I found myself that tutorial earlyer, but I have another question...where do I implement this 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 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
found the answer:D
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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180111.1 | Last Updated 17 Jan 2011
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100