Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Color of Pixel at a particular point on a form?

Status
Not open for further replies.

cjtaylor

Programmer
Aug 12, 2001
69
US
Hello,

Is there an efficient way to get the System.Color of a particular Point(x,y)? I have found a way to do this if save the form as a bitmap and then call bmp.GetPixel(x,y), but this is extremely inefficient way of doing it. Any ideas?

Thanks,
Chris Taylor
 
I didnt saw a better way till now.

________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
I'm sure you've already got this far, but I'll post the code anyway.

The desktop capture code is from Syncfusions FAQ, but the bitmap size is only 1 pixel to keep the size down.
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace ColorPicker
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form, IMessageFilter
	{
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.PictureBox pictureBox2;
		private System.Windows.Forms.PictureBox pictureBox3;
		private System.Windows.Forms.PictureBox pictureBox4;
		private System.Windows.Forms.PictureBox pictureBox5;
		private System.Windows.Forms.PictureBox pictureBox6;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			Application.AddMessageFilter(this);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.pictureBox2 = new System.Windows.Forms.PictureBox();
			this.pictureBox3 = new System.Windows.Forms.PictureBox();
			this.pictureBox4 = new System.Windows.Forms.PictureBox();
			this.pictureBox5 = new System.Windows.Forms.PictureBox();
			this.pictureBox6 = new System.Windows.Forms.PictureBox();
			this.SuspendLayout();
			// 
			// pictureBox1
			// 
			this.pictureBox1.BackColor = System.Drawing.Color.Red;
			this.pictureBox1.Location = new System.Drawing.Point(80, 80);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.TabIndex = 0;
			this.pictureBox1.TabStop = false;
			// 
			// pictureBox2
			// 
			this.pictureBox2.BackColor = System.Drawing.Color.Green;
			this.pictureBox2.Location = new System.Drawing.Point(200, 80);
			this.pictureBox2.Name = "pictureBox2";
			this.pictureBox2.TabIndex = 0;
			this.pictureBox2.TabStop = false;
			// 
			// pictureBox3
			// 
			this.pictureBox3.BackColor = System.Drawing.Color.Blue;
			this.pictureBox3.Location = new System.Drawing.Point(312, 80);
			this.pictureBox3.Name = "pictureBox3";
			this.pictureBox3.TabIndex = 0;
			this.pictureBox3.TabStop = false;
			// 
			// pictureBox4
			// 
			this.pictureBox4.BackColor = System.Drawing.Color.Cyan;
			this.pictureBox4.Location = new System.Drawing.Point(80, 144);
			this.pictureBox4.Name = "pictureBox4";
			this.pictureBox4.TabIndex = 0;
			this.pictureBox4.TabStop = false;
			// 
			// pictureBox5
			// 
			this.pictureBox5.BackColor = System.Drawing.Color.Magenta;
			this.pictureBox5.Location = new System.Drawing.Point(200, 144);
			this.pictureBox5.Name = "pictureBox5";
			this.pictureBox5.TabIndex = 0;
			this.pictureBox5.TabStop = false;
			// 
			// pictureBox6
			// 
			this.pictureBox6.BackColor = System.Drawing.Color.Yellow;
			this.pictureBox6.Location = new System.Drawing.Point(312, 144);
			this.pictureBox6.Name = "pictureBox6";
			this.pictureBox6.TabIndex = 0;
			this.pictureBox6.TabStop = false;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(496, 286);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.pictureBox2);
			this.Controls.Add(this.pictureBox3);
			this.Controls.Add(this.pictureBox4);
			this.Controls.Add(this.pictureBox5);
			this.Controls.Add(this.pictureBox6);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		#region IMessageFilter Members

		private const int WM_MOUSEMOVE = 0x200;

		private const int SRCCOPY = 0xCC0020;

		[DllImport("user32.dll")] public extern static IntPtr GetDesktopWindow(); 
 
		[DllImport("user32.dll")] public static extern IntPtr GetWindowDC(IntPtr hwnd); 
 
		[DllImport("gdi32.dll")] public static extern UInt64 BitBlt (IntPtr hDestDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, System.Int32 dwRop); 

		public bool PreFilterMessage(ref Message m)
		{
			if (m.Msg == WM_MOUSEMOVE)
			{
				Point p = this.PointToScreen(this.PointToClient(Cursor.Position));

				Bitmap bmp = new Bitmap(1, 1); 
 
				Graphics gr1 = Graphics.FromImage(bmp); 
 
				IntPtr dc1 = gr1.GetHdc(); 
 
				IntPtr dc2 = GetWindowDC(GetDesktopWindow()); 
 
				BitBlt(dc1, 0, 0, 1, 1, dc2, p.X, p.Y, SRCCOPY); 
 
				gr1.ReleaseHdc(dc1);

				Console.WriteLine(bmp.GetPixel(0, 0).ToString());
			}

			return false;
		}

		#endregion
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top