|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.InteropServices;
- using System.Drawing;
- namespace MarkRecognized
- {
- static class WIN32API
- {
- #region 获取屏幕上任意一点的颜色
- /*
- * code from
- http://dcxl.blog.sohu.com/113453543.html
- http://zhidao.baidu.com/question/372951254.html
- * changed somewhere needed.
- */
- [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
- private static extern IntPtr GetDC(IntPtr hwnd);
- [DllImport("gdi32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
- private static extern uint GetPixel(IntPtr hdc, int x, int y);
- [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)] //确定坐标
- private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
- [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
- private static extern int ScreenToClient(int hwnd, ref Point lpPoint);
- [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
- private static extern int WindowFromPoint(int xPoint, int yPoint);
- public static System.Drawing.Color GetPixelFromScreenPoint(int x, int y)
- {
- return GetPixelFromPoint(x, y, IntPtr.Zero);
- }
- public static System.Drawing.Color GetPixelFromPoint(int x, int y,IntPtr Dc)
- {
- IntPtr hdc = GetDC(Dc);
- uint pixel = GetPixel(hdc, x, y);
- ReleaseDC(Dc, hdc);
- Color color =
- Color.FromArgb(
- (int)(pixel & 0xFF000000) >> 24,
- (int)(pixel & 0x000000FF),
- (int)(pixel & 0x0000FF00) >> 8,
- (int)(pixel & 0x00FF0000) >> 16);
- return color;
- }
- public static System.Drawing.Color GetPixelFromPoint(Point p, IntPtr Dc)
- {
- return GetPixelFromPoint(p.X, p.Y, Dc);
- }
- public static System.Drawing.Color GetPixelFromScreenPoint(Point p)
- {
- return GetPixelFromPoint(p, IntPtr.Zero);
- }
- #endregion
- }
- }
复制代码- private void IPCam_MouseMove(object sender, MouseEventArgs e)
- {
- try
- {
- Color currentColor = WIN32API.GetPixelFromPoint(e.Location, this.IPCam.Handle);
- this.toolStripStatusLabelColorSnap.Text = currentColor.ToString();
- this.toolStripStatusLabelShowColor.ForeColor = currentColor;
- }
- catch { }
- }
复制代码 |
|