如何使用c#获取X,Y像素的颜色?如何使用c#获得X,Y像素的颜色?
至于结果,我可以将结果转换为我需要的颜色格式。我确信有这个API调用。
“对于监视器上的任何给定的X,Y,我想获取该像素的颜色。”
如何使用c#获取X,Y像素的颜色?如何使用c#获得X,Y像素的颜色?
至于结果,我可以将结果转换为我需要的颜色格式。我确信有这个API调用。
“对于监视器上的任何给定的X,Y,我想获取该像素的颜色。”
要从屏幕得到一个像素的颜色这里的代码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;
}
}
有Bitmap.GetPixel
为图像...是你在追求什么?如果不是,你能说出你的哪个x,y值吗?在控制?
请注意,如果你做意味着图像,你想获得大量像素,你不介意与不安全的代码工作,那么Bitmap.LockBits
会比很多调用快很多GetPixel
。
我需要它从当前的显示,而不是一个特定的文件,或油漆盒的实例。 – MichaelICE 2009-04-15 18:52:55
除了从P/Invoke的解决方案,你可以使用Graphics.CopyFromScreen从屏幕上的图像数据进入一个图形对象。如果您不担心可移植性,我会推荐P/Invoke解决方案。
在WPF参考:(PointToScreen的使用)
System.Windows.Point position = Mouse.GetPosition(lightningChartUltimate1);
if (lightningChartUltimate1.ViewXY.IsMouseOverGraphArea((int)position.X, (int)position.Y))
{
System.Windows.Point positionScreen = lightningChartUltimate1.PointToScreen(position);
Color color = WindowHelper.GetPixelColor((int)positionScreen.X, (int)positionScreen.Y);
Debug.Print(color.ToString());
...
...
public class WindowHelper
{
// ******************************************************************
[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.Windows.Media.Color GetPixelColor(int x, int y)
{
IntPtr hdc = GetDC(IntPtr.Zero);
uint pixel = GetPixel(hdc, x, y);
ReleaseDC(IntPtr.Zero, hdc);
Color color = Color.FromRgb(
(byte)(pixel & 0x000000FF),
(byte)((pixel & 0x0000FF00) >> 8),
(byte)((pixel & 0x00FF0000) >> 16));
return color;
}
在屏幕上或者在您的应用程序窗口这个地方? – ChrisF 2009-04-15 18:48:53
只是一般的显示。我不关心任何特定的事例。 – MichaelICE 2009-04-15 18:53:56
对于监视器上的任何给定的X,Y,我想获取该像素的颜色。 – MichaelICE 2009-04-15 18:55:10