Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
1.00/5 (1 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
Comments
Manfred Rudolf Bihy 17-Jan-11 20:11pm    
See my modified answer for a code example and start studying MSDN .NET documentation.

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:

C#
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);
}
 
Share this answer
 
Comments
Manfred Rudolf 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.
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!

C#
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
 
Share this answer
 
v2
Comments
Manfred Rudolf 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 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 Rudolf 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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900