Click here to Skip to main content
13,148,503 members (51,128 online)
Rate this:
Please Sign up or sign in to vote.
I want to make a software for Foot pressure measurement and I am using a Device in which a camera is integrated and that show the foot impression.

Till now I am using dll for capture image from device and convert that image into grayscale and after that find out color on every pixel and set color according to dark gray is found in image.

But It is not work I have done following code for it and is it a right direction to process it?

private void cmb_cameras_SelectedIndexChanged(object sender, EventArgs e)
    //selected video source
    FinalVideoSource = new VideoCaptureDevice(VideoCaptureDevices[cmb_cameras.SelectedIndex].MonikerString);
    FinalVideoSource.NewFrame += new NewFrameEventHandler(FinalVideoSource_NewFrame);
void FinalVideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
        //System.Console.WriteLine("1: Flag status: " + flag.ToString());
        if (flag!=true)
            flag = true;
            //System.Console.WriteLine("2: Flag status: "+ flag.ToString());
            lock (NewBitmap)
                using (ImageAttributes attributes = new ImageAttributes())
                    LiveImage = (Bitmap)eventArgs.Frame.Clone();
                    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}
                    //draw the original image on the new image
                    //using the grayscale color matrix
                    g.DrawImage(LiveImage, new Rectangle(0, 0, 640, 480),
                      0, 0, 640, 480, GraphicsUnit.Pixel, attributes);
     //--------------------------- Method 2ND ----------------------------------
                   // System.Diagnostics.Stopwatch sWatch = new System.Diagnostics.Stopwatch();
                   // sWatch.Start();
                   // PB_LiveImage.Image = LiveImage;
                    PB_LiveImage.Image = ChangeColor();
    catch (Exception ex)
        MessageBox.Show("Message:\n\nKindly closed the application.\nOne unhandled exception has been occurred!!");
public unsafe Bitmap ChangeColor()
    Bitmap bmp = new Bitmap(NewBitmap);
    LockBitClass lockBitmap = new LockBitClass(bmp);
    Color compareClr = Color.FromArgb(75, 75, 75);
    Color compareClr2 = Color.FromArgb(90, 90, 90);
    Color compareClr3 = Color.FromArgb(105, 105, 105);
    Color compareClr4 = Color.FromArgb(150, 150, 150);
    for (int y = 0; y < lockBitmap.Height; y++)
        for (int x = 0; x < lockBitmap.Width; x++)
            //Console.WriteLine((System.Drawing.Color.LightGray.ToArgb() == lockBitmap.GetPixel(x, y).ToArgb()) + " LightGray");
            //Console.WriteLine((System.Drawing.Color.Gray.ToArgb() == lockBitmap.GetPixel(x, y).ToArgb()) + " Gray");
            //Console.WriteLine((System.Drawing.Color.DarkGray.ToArgb() == lockBitmap.GetPixel(x, y).ToArgb()) + " DarkGray");
            if (System.Drawing.Color.LightGray.ToArgb() == lockBitmap.GetPixel(x, y).ToArgb())                        
                lockBitmap.SetPixel(x, y, Color.Cyan);
            else if (System.Drawing.Color.Gray.ToArgb() == lockBitmap.GetPixel(x, y).ToArgb())
                lockBitmap.SetPixel(x, y, Color.Yellow);
            else if (System.Drawing.Color.DarkGray.ToArgb() == lockBitmap.GetPixel(x, y).ToArgb())
                lockBitmap.SetPixel(x, y, Color.Red);
                lockBitmap.SetPixel(x, y, Color.Blue);
    return bmp;
Posted 15-Mar-13 1:09am
Updated 15-Mar-13 20:41pm
To start with, never use SetPixel/GetPixel, unless you need to process very few.
rajesh@1989 16-Mar-13 2:49am
Thank you Sergey for your response!!

Then How do I do without SetPixel/GetPixel?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

rajesh@1989 asked:
Then How do I do without SetPixel/GetPixel?
The whole idea is to call System.Drawing.Bitmap.LockBits and use System.Drawing.Imaging.BitmapData returned from this call to directly manipulate locked image memory. Look at the code sample to see how to do it:[^].

In your code sample, your call to LockBits is totally pointless. You don't do anything to your bitmap. When you actually do it in the way shown in the code sample referenced above, it will solve all your problem.

As to GetPixel/SePixel, not only these calls totally defeat the purpose of LockBits, they also prohibitively slow and should only be used if you want to read or modify very few pixels, in a very simplified way.


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 |
Web01 | 2.8.170924.2 | Last Updated 16 Mar 2013
Copyright © CodeProject, 1999-2017
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