Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Now I want to make color recognition for picture that stored in hard disk. I want to separate color (which pixel is blue, which pixel is red and so on). I used Visual Studio 2008 . Is it possible to develop it? Anyone make me suggestion , please.
Posted
Updated 28-Nov-11 21:54pm
v4

go through the below code this may helps you

using System;
using System.Drawing;
using System.Windows.Forms;

namespace GetPixelExample
{
    public class Form1 : Form
    {
        private class ImageControl : Panel
        {
            private Bitmap bitmap = new Bitmap("C:\\alien.jpg");

            public ImageControl()
            {
                MouseMove += new MouseEventHandler(HandleMouseMove);
            }

            private void HandleMouseMove(object sender, MouseEventArgs e)
            {
                // Scale the window coordinates to the bitmap coordinates
                double windowX = e.X;
                double windowY = e.Y;

                double controlWidth = Width;
                double controlHeight = Height;
                double imageWidth = bitmap.Width;
                double imageHeight = bitmap.Height;

                Color pixel = bitmap.GetPixel(
                   (int)(windowX * bitmap.Width / controlWidth), 
                   (int)(windowY * bitmap.Height / controlHeight));
                System.Diagnostics.Debug.WriteLine(pixel);
            }

            protected override void OnResize(EventArgs eventargs)
            {
                base.OnResize(eventargs);
                Invalidate();
            }

            protected override void  OnPaintBackground(PaintEventArgs pevent)
            {
                 pevent.Graphics.DrawImage(bitmap, Bounds);
            }
        }

        public Form1()
        {
            ImageControl control = new ImageControl();
            control.Dock = DockStyle.Fill;
            Controls.Add(control);
            this.PerformLayout();
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
 
Share this answer
 
Yes, it it possible. But it may not be as simple as you think - it may depend on what the file is stored as. A bitmap is easy: a pixel is a pixel. A jpeg is not so easy: the data is compressed using a "lossy" algorithm, so what starts out as a red pixel next to a blue pixel may become a magenta pixel.

Either way, the first place to start is to load your image:
Image i = Image.FromFile(path);

Then look at each pixel individually:
int pixel = i.GetPixel(x, y);
The pixel then contains the ARGB value.
int Red = (pixel >> 16) & 0xFF;
int Green = (pixel >> 8) & 0xFF;
int Blue = pixel & 0xFF;
 
Share this answer
 

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