Here's something I've just thrown together which should do what you're asking for:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace ShowColorTest
{
public class Form1
{
private IContainer components = null;
private PictureBox pictureBox1;
private Button button1;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
components.Dispose();
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.pictureBox1 = new PictureBox();
this.button1 = new Button();
((ISupportInitialize)this.pictureBox1).BeginInit();
this.SuspendLayout();
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.Controls.Add(this.button1);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
((ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
public Form1()
{
InitializeComponent();
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (this.pictureBox1.Image == null)
{
MessageBox.Show("load a picture first!");
return;
}
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);
MessageBox.Show(sb.ToString());
}
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));
}
}
}
}