C# API 捕获屏幕点颜色

        private Color GetPointColor(Point ColorPoint)//获取点颜色
        {
            Color PointColor;
            // 创建显示器的DC
            IntPtr hdlDisplay = CreateDC("DISPLAY", null, null, IntPtr.Zero);
            // 从指定设备的句柄创建新的 Graphics 对象
            Graphics gfxDisplay = Graphics.FromHdc(hdlDisplay);
            // 创建只有一个象素大小的 Bitmap 对象
            Bitmap bmp = new Bitmap(1, 1, gfxDisplay);
            // 从指定 Image 对象创建新的 Graphics 对象
            Graphics gfxBmp = Graphics.FromImage(bmp);
            // 获得屏幕的句柄
            IntPtr hdlScreen = gfxDisplay.GetHdc();
            // 获得位图的句柄
            IntPtr hdlBmp = gfxBmp.GetHdc();
            // 把当前屏幕中鼠标指针所在位置的一个象素拷贝到位图中
            BitBlt(hdlBmp,0, 0, 1, 1, hdlScreen, 0, 0, 13369376);
            // 释放屏幕句柄
            gfxDisplay.ReleaseHdc(hdlScreen);
            // 释放位图句柄
            gfxBmp.ReleaseHdc(hdlBmp);
            PointColor = bmp.GetPixel(0, 0); // 获取像素的颜色
            labelRGB.Text = "0x" + PointColor.ToArgb().ToString("x").ToUpper();
            gfxDisplay.Dispose();
            gfxBmp.Dispose();
            bmp.Dispose(); // 释放 bmp 所使用的资源
            return PointColor;
        }

转载于:https://www.cnblogs.com/yxbsmx/articles/1280322.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
捕获屏幕输入焦点,你可以使用以下代码: ```csharp using System; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; public class ScreenCaptureForm : Form { // 变量声明 private Rectangle _captureRect; private bool _capturing; private Point _startPoint; private Point _endPoint; // 构造函数 public ScreenCaptureForm() { this.DoubleBuffered = true; this.FormBorderStyle = FormBorderStyle.None; this.WindowState = FormWindowState.Maximized; this.Cursor = Cursors.Cross; this.MouseDown += new MouseEventHandler(ScreenCaptureForm_MouseDown); this.MouseMove += new MouseEventHandler(ScreenCaptureForm_MouseMove); this.Paint += new PaintEventHandler(ScreenCaptureForm_Paint); this.KeyDown += new KeyEventHandler(ScreenCaptureForm_KeyDown); } // 捕获屏幕方法 public Image CaptureScreen(Rectangle rect) { Bitmap bmp = new Bitmap(rect.Width, rect.Height); using (Graphics g = Graphics.FromImage(bmp)) { g.CopyFromScreen(rect.Location, Point.Empty, rect.Size); } return bmp; } // 绘制截图框方法 private void DrawCaptureRect(Graphics g) { using (Pen pen = new Pen(Color.Red, 2)) { g.DrawRectangle(pen, _captureRect); } } // 鼠标按下事件 private void ScreenCaptureForm_MouseDown(object sender, MouseEventArgs e) { _capturing = true; _startPoint = e.Location; } // 鼠标移动事件 private void ScreenCaptureForm_MouseMove(object sender, MouseEventArgs e) { if (_capturing) { _endPoint = e.Location; _captureRect.Location = new Point( Math.Min(_startPoint.X, _endPoint.X), Math.Min(_startPoint.Y, _endPoint.Y)); _captureRect.Size = new Size( Math.Abs(_startPoint.X - _endPoint.X), Math.Abs(_startPoint.Y - _endPoint.Y)); this.Invalidate(); } } // 绘制事件 private void ScreenCaptureForm_Paint(object sender, PaintEventArgs e) { if (_capturing) { DrawCaptureRect(e.Graphics); } } // 按键事件 private void ScreenCaptureForm_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Escape) { this.Close(); } else if (e.KeyCode == Keys.Enter) { Image img = CaptureScreen(_captureRect); img.Save("screenshot.png", System.Drawing.Imaging.ImageFormat.Png); this.Close(); } } // 程序入口 static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new ScreenCaptureForm()); } } ``` 这个代码实现了一个简单的屏幕截图工具,当你运行它时,会在屏幕上显示一个红色的矩形框,你可以用鼠标拖动这个框来选择截图区域。按下 Enter 键时,程序会将选中的区域截图保存到当前目录下的 screenshot.png 文件中。如果按下 Esc 键则退出程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值