Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 11:43am
Comments
Manfred R. Bihy at 17-Jan-11 20:11pm
   
See my modified answer for a code example and start studying MSDN .NET documentation.
Rate this: bad
good
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);
}
  Permalink  
Comments
Manfred R. Bihy at 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
good
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: 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
  Permalink  
v2
Comments
Manfred R. Bihy at 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 at 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 at 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 at 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 at 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 at 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 at 17-Jan-11 21:05pm
   
found the answer:D
Manfred R. Bihy at 17-Jan-11 21:13pm
   
Good, keep on going!
Member 1493570 at 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 at yesterday
   
I'm also having the same problem. Did you managed to get a solution for 8bpp?

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

  Print Answers RSS
0 Maciej Los 589
1 OriginalGriff 379
2 CPallini 205
3 RyanDev 175
4 Sergey Alexandrovich Kryukov 160
0 OriginalGriff 5,689
1 Sergey Alexandrovich Kryukov 4,783
2 Peter Leow 2,954
3 Maciej Los 2,819
4 DamithSL 2,465


Advertise | Privacy | Mobile
Web03 | 2.8.140718.1 | Last Updated 17 Jan 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid