Graphics in C#
Is it possible to make a program which will recognize colors in a picture (.jpg, .gif or any other type). Eg if I click on a certain part of a picture, I want the to see the message telling me which color I'v clicked on and to store that info in a string for further use.
I have no knowledge of the API's in C#, but this is what you need:
1. Load and draw image on screen.
2. A way to identify the location clicked.
3. Use something like "GetPixel" for the location.
4. Display/store the R, G, B values.
None of this looks to me like it's rocket science.
System.Drawing.Bitmap has a GetPixel method.
ok thx I'll see if this does the trick
Here's something I've just thrown together which should do what you're asking for:
public class Form1
private IContainer components = null;
private PictureBox pictureBox1;
private Button button1;
protected override void Dispose(bool disposing)
if (disposing && (components != null))
private void InitializeComponent()
this.pictureBox1 = new PictureBox();
this.button1 = new Button();
this.pictureBox1.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right)));
this.pictureBox1.Location = new Point(12, 12);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new Size(260, 215);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.MouseClick += new MouseEventHandler(this.pictureBox1_MouseClick);
this.button1.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
this.button1.Location = new Point(197, 233);
this.button1.Name = "button1";
this.button1.Size = new Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "load picture";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new EventHandler(this.button1_Click);
this.AutoScaleDimensions = new SizeF(6F, 13F);
this.AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new Size(284, 264);
this.Name = "Form1";
this.Text = "Form1";
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
if (this.pictureBox1.Image == null)
MessageBox.Show("load a picture first!");
Color c = ((Bitmap)this.pictureBox1.Image).GetPixel(e.X, e.Y);
StringBuilder sb = new StringBuilder();
sb.AppendLine("Results for co-ordinate: " + e.X + ", " + e.Y);
sb.AppendLine("R = " + c.R + ", G = " + c.G + ", B = " + c.B);
private void button1_Click(object sender, EventArgs e)
OpenFileDialog o = new OpenFileDialog();
o.Multiselect = false;
o.Filter = "Supported file types (*.bmp;*.jpg;*.gif)|*.bmp;*.jpg;*.gif|Bitmap (*.bmp)|*.bmp|JPeg (*.jpg)|*.jpg|Gif (*.gif)|*.gif";
if (o.ShowDialog() == DialogResult.OK)
byte buffer = File.ReadAllBytes(o.FileName);
this.pictureBox1.Image = new Bitmap(new MemoryStream(buffer));
Wow thanks, this is realy something, I'll definitely try this. Is it possible to apply this to a wider area, or just to one pixel.
I'm supposed to show basic AI movements (chasing, evading and patroling) in a simple example - this is my task for the end of 3. year at ETF university in Croatia (sort of pre-diploma paper). The problem is I'v never had any lessons in programing graphics. So I'll try to make a program that will recognize obstacles by their color (eg you can pass trough white, but not trough red etc.). I know this is nowhere near the real way this gets done, but for obvious reason I have to be creative. Of course I appreciate your help, and if you have any suggestions how this could be done easier please reply