using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Resources; using System.Reflection;namespace GetColor { public partial class Form1 : Form { private Bitmap bitmap;
然后用Graphics.CopyFromScreen把屏幕拷贝上去.
接下来要取位于(x,y)的颜色就很简单了.
直接调用Bitmap.GetPixel (x,y);
BitBlt: 获取屏幕上一块区域的截图。
使用timer来做..
private void timer1_Tick(object sender, EventArgs e)
{
POINT _Point = new POINT(); GetCursorPos(ref _Point); IntPtr _Hwnd = WindowFromPoint((uint)_Point.x, (uint)_Point.y); IntPtr _DC = GetDC(_Hwnd);
ScreenToClient(_Hwnd, ref _Point);
uint _ColorLong = GetPixel(_DC, _Point.x, _Point.y);
Color _Color = Color.FromArgb((int)_ColorLong & 0xFF, (int)(_ColorLong & 0xFF00) / 256, (int)(_ColorLong & 0xFF0000) / 65536);
ReleaseDC(_Hwnd, _DC);
this.BackColor = _Color;
} [DllImport("user32.dll")]
public static extern IntPtr WindowFromPoint(uint x_point, uint y_point); [DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetDC(IntPtr hWnd); [DllImport("user32.dll")]
public static extern uint ScreenToClient(IntPtr hwnd, ref POINT p_Point); [DllImport("gdi32.dll")]
public static extern uint GetPixel(IntPtr hDC, int XPos, int YPos); [DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); [DllImport("user32.dll")]
public static extern bool GetCursorPos(ref POINT p_Point); [StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
}
那个方法已经可以屏幕取任意地方的颜色了.
你再加个timer结合Cursor.Position就能
用鼠标随时取屏幕任何地方的颜色了.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;namespace GetColor
{
public partial class Form1 : Form
{
private Bitmap bitmap;
public Form1()
{
InitializeComponent();
} private void GetColor()
{
Point mouseP = Cursor.Position;
Screen screen = Screen.PrimaryScreen;
if (bitmap == null)
bitmap = new Bitmap(screen.Bounds.Width, screen.Bounds.Height);
Graphics g = Graphics.FromImage(bitmap);
g.CopyFromScreen(Point.Empty, Point.Empty, screen.Bounds.Size);
g.DrawImage(bitmap, Point.Empty);
Color c = bitmap.GetPixel(mouseP.X, mouseP.Y);
string currColor = Convert.ToString(ColorTranslator.ToHtml(c));
txtColor.BackColor = c;
txtRGB.Text = Convert.ToString(ColorTranslator.ToHtml(c)); } private void stopGetColor()
{
timer1.Stop();
GetColor();
} private void timer1_Tick(object sender, EventArgs e)
{
GetColor();
} private void Form1_Load(object sender, EventArgs e)
{
} private void btnOK_Click(object sender, EventArgs e)
{
timer1.Start();
btnOK.Enabled = false;
}
}
}可以取色了就是不知道如何停止取色想双击鼠标停止取色,可是双击事件只是在窗体上双击才有效,在窗体外部不可以呀。。这个该怎么弄呀?
int mouseY = e.Y;
Color pixelColor = myBitmap.GetPixel(mouseX, mouseY);
colorbox.BackColor = pixelColor;
参考