开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 1609|回复: 3
收起左侧

[C#图文教程] WIN32API 获得特定坐标的像素

[复制链接]

结帖率:33% (1/3)
发表于 2013-2-18 19:21:03 | 显示全部楼层 |阅读模式   上海市上海市
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. using System.Drawing;

  7. namespace MarkRecognized
  8. {
  9.     static class WIN32API
  10.     {
  11.         #region 获取屏幕上任意一点的颜色
  12.         /*
  13.          * code from
  14.         http://dcxl.blog.sohu.com/113453543.html
  15.         http://zhidao.baidu.com/question/372951254.html
  16.          * changed somewhere needed.
  17.           */
  18.         [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
  19.         private static extern IntPtr GetDC(IntPtr hwnd);

  20.         [DllImport("gdi32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
  21.         private static extern uint GetPixel(IntPtr hdc, int x, int y);

  22.         [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)] //确定坐标
  23.         private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);

  24.         [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
  25.         private static extern int ScreenToClient(int hwnd, ref Point lpPoint);

  26.         [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
  27.         private static extern int WindowFromPoint(int xPoint, int yPoint);





  28.         public static System.Drawing.Color GetPixelFromScreenPoint(int x, int y)
  29.         {
  30.             return GetPixelFromPoint(x, y, IntPtr.Zero);
  31.         }

  32.         public static System.Drawing.Color GetPixelFromPoint(int x, int y,IntPtr Dc)
  33.         {
  34.             IntPtr hdc = GetDC(Dc);
  35.             uint pixel = GetPixel(hdc, x, y);
  36.             ReleaseDC(Dc, hdc);
  37.             Color color =
  38.                 Color.FromArgb(
  39.                 (int)(pixel & 0xFF000000) >> 24,
  40.                 (int)(pixel & 0x000000FF),
  41.                 (int)(pixel & 0x0000FF00) >> 8,
  42.                 (int)(pixel & 0x00FF0000) >> 16);
  43.             return color;
  44.         }

  45.         public static System.Drawing.Color GetPixelFromPoint(Point p, IntPtr Dc)
  46.         {
  47.             return GetPixelFromPoint(p.X, p.Y, Dc);
  48.         }

  49.         public static System.Drawing.Color GetPixelFromScreenPoint(Point p)
  50.         {
  51.             return GetPixelFromPoint(p, IntPtr.Zero);
  52.         }
  53.         #endregion
  54.     }
  55. }
复制代码
  1. private void IPCam_MouseMove(object sender, MouseEventArgs e)
  2. {
  3.     try
  4.     {
  5.         Color currentColor = WIN32API.GetPixelFromPoint(e.Location, this.IPCam.Handle);
  6.         this.toolStripStatusLabelColorSnap.Text = currentColor.ToString();
  7.         this.toolStripStatusLabelShowColor.ForeColor = currentColor;

  8.     }
  9.     catch { }
  10. }
复制代码
发表于 2013-4-20 10:07:58 | 显示全部楼层   山西省太原市
记一下吧,虽然不会用,但是必经之路啊
回复 支持 反对

使用道具 举报

结帖率:50% (3/6)

签到天数: 22 天

发表于 2013-2-18 20:03:23 | 显示全部楼层   湖北省荆门市
这个是什么啊?不是易语言?
回复 支持 反对

使用道具 举报

结帖率:100% (16/16)

签到天数: 15 天

发表于 2013-2-18 19:30:12 | 显示全部楼层   四川省成都市
沙发  我是为了 几分,,,,,,沙发有几分
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表