如何使用c#获取X,Y处像素的颜色?
至于结果,我可以将结果转换为我需要的颜色格式.我确信有一个API调用.
对于显示器上任何给定的X,Y,我想获得该像素的颜色.
CLa*_*RGe 39
要从屏幕获取像素颜色,请从Pinvoke.net获取代码:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
sealed class Win32
{
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("gdi32.dll")]
static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
static public System.Drawing.Color GetPixelColor(int x, int y)
{
IntPtr hdc = GetDC(IntPtr.Zero);
uint pixel = GetPixel(hdc, x, y);
ReleaseDC(IntPtr.Zero, hdc);
Color color = Color.FromArgb((int)(pixel & 0x000000FF),
(int)(pixel & 0x0000FF00) >> 8,
(int)(pixel & 0x00FF0000) >> 16);
return color;
}
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 11
有Bitmap.GetPixel一个图像...是你所追求的?如果没有,你能说出你的x,y值吗?在控制?
需要注意的是,如果你这样做意味着图像,你想获得大量的像素,而你不介意与不安全的代码工作,那么Bitmap.LockBits会比很多调用的快得多GetPixel.
| 归档时间: |
|
| 查看次数: |
44251 次 |
| 最近记录: |