110,533
社区成员
发帖
与我相关
我的任务
分享
public static object isOut = "obj";
private void timer1_Tick(object sender, EventArgs e)
{
lock (isOut)
{
if (isOut.ToString() == "now")
return;
isOut = "now";
Bitmap bmp = null;
Graphics gfxDisplay = null;
Graphics gfxBmp = null;
try
{
//创建显示器的DC
IntPtr hdlDisplay = CreateDC("DISPLAY", null, null, IntPtr.Zero);
if (hdlDisplay != null)
{
//从指定设备的句柄创建新的 Graphics 对象
gfxDisplay = Graphics.FromHdc(hdlDisplay);
if (gfxDisplay != null)
{
//创建只有一个象素大小的 Bitmap 对象
bmp = new Bitmap(1, 1, gfxDisplay);
if (bmp != null)
{
//从指定 Image 对象创建新的 Graphics 对象
gfxBmp = Graphics.FromImage(bmp);
if (gfxBmp != null)
{
//得到屏幕句柄
IntPtr hdlScreen = gfxDisplay.GetHdc();
if (hdlScreen != null)
{
//得到位图句柄
IntPtr hdlBmp = gfxBmp.GetHdc();
if (hdlBmp != null)
{
//把当前屏幕中鼠标指针所在位置的一个象素拷贝到位图中
BitBlt(hdlBmp, 0, 0, 1, 1, hdlScreen, MousePosition.X, MousePosition.Y, 13369376);
//释放屏幕句柄
gfxDisplay.ReleaseHdc(hdlScreen);
//释放位图句柄
gfxBmp.ReleaseHdc(hdlBmp);
//获取象素的颜色
Color c = bmp.GetPixel(0, 0);
panelColorBox.BackColor = c;
txtWeb.Text = ColorTranslator.ToHtml(c);
txtRGB.Text = c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString();
if (gfxDisplay != null)
gfxDisplay.Dispose();
if (gfxBmp != null)
gfxBmp.Dispose();
if (bmp != null)
bmp.Dispose();//释放BMP使用的资源
}
}
}
}
}
}
}
catch (Exception ex)
{
string test = ex.ToString();
StreamWriter log = new StreamWriter(@"C:\Users\USER\Desktop\颜色捕捉器\ceshi.txt", true);
log.WriteLine("异常发生时间:" + DateTime.Now + "\r\n" + test + "\r\n");
log.Close();
this.Dispose();
this.Close();
}
finally
{ //MessageBox.Show(ex.Message);
if (gfxDisplay != null)
gfxDisplay.Dispose();
if (gfxBmp != null)
gfxBmp.Dispose();
if (bmp != null)
bmp.Dispose();//释放BMP使用的资源
}
isOut = "ok";
}
}
//定义工儿区的尺寸
Size workingArea;
//窗体的初始位置和左下角及右下角的位置
Point ptLeftBottom, ptRightBottom;
//Point formLoc;
public Form1()
{
InitializeComponent();
FormBorderStyle = FormBorderStyle.FixedToolWindow;
ShowInTaskbar = false;
this.StartPosition = FormStartPosition.CenterScreen;
//获取屏幕工作区域的大小
Rectangle rect = SystemInformation.WorkingArea;
workingArea = new Size(rect.Width, rect.Height);
ptLeftBottom = new Point(0, workingArea.Height - this.Height);
ptRightBottom = new Point(workingArea.Width - this.Width, workingArea.Height = this.Height);
string tipmsg = "F8开始取色,按ESC停止取色";
toolTip1.SetToolTip(this, tipmsg);
toolTip1.SetToolTip(panelColorBox, tipmsg);
toolTip1.SetToolTip(txtWeb, tipmsg);
toolTip1.SetToolTip(lblRGB, tipmsg);
toolTip1.SetToolTip(lblWeb, tipmsg);
}
//释放屏幕句柄
gfxDisplay.ReleaseHdc(hdlScreen);
//释放位图句柄
gfxBmp.ReleaseHdc(hdlBmp);
public partial class Form2 : Form
{
public Form2() {
InitializeComponent();
Timer timer = new Timer();
timer.Tick += timer_Tick;
timer.Interval = 50;
timer.Start();
}
private void timer_Tick(object sender, EventArgs e) {
Color clr;
using (Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)) {
using (Graphics g = Graphics.FromImage(bmp)) {
g.CopyFromScreen(0, 0, 0, 0, bmp.Size); //获取屏幕图像
}
this.Text = (clr = bmp.GetPixel(MousePosition.X, MousePosition.Y)).ToString(); //窗体的标题上显示 rgb
using (Graphics g = this.CreateGraphics()) {
g.DrawImage(bmp,
new Rectangle(0, 0, 41, 41), //绘制到窗体的区域
new Rectangle(MousePosition.X - 20, MousePosition.Y - 20, 41, 41), //需要绘制的原图
GraphicsUnit.Pixel); //注意 按理来说 这里的图像 应该放大绘制 这里省去了
using (SolidBrush sb = new SolidBrush(clr)) {
g.FillRectangle(sb, 45, 0, 20, 20); //用对应颜色绘制一个矩形框
}
}
}
}
}