屏幕上点的颜色(像素) [英] Color of a point (pixel) on a screen
问题描述
我需要确定表单上各个点(像素)的颜色,包括我添加的图形形状(线,圆等).在VB6中这很容易,但是在vb.net 10中似乎很难.
我在Google搜索中找到了以下代码段. CopyFromScreen函数实际上可以将1个像素的矩形复制到0,0点,然后读取其颜色.但是,当我尝试用实际的X,Y坐标(例如(10,10)甚至是(me.location.x,me.location.y))替换Windows.Forms.Cursor.Postion时,我收到一条错误消息,指出整数可以与CopyFromScreen一起使用.
关于为什么我不能用数字或某种声明的变量类型替换Windows.Forms.Cursor.Postion的任何想法?
一如既往,在此先感谢您的帮助!
加里·沃格尔
公共课程表格1
私有WithEvents timer1作为新的Windows.Forms.Timer
私有子Form1_Load(ByVal发送者为System.Object,_
ByVal e As System.EventArgs)处理MyBase.Load
timer1.Interval = 10
timer1.Start()
结束子
私人Sub Timer1_Tick(ByVal发送者为System.Object,_
ByVal e As System.EventArgs)处理timer1.Tick
将bmp用作新的位图(1,1)
使用g作为Graphics = Graphics.FromImage(bmp)
g.CopyFromScreen(Windows.Forms.Cursor.Position,_
新点(0,0),新大小(1,1))
最终使用
Me.Text = bmp.GetPixel(0,0).ToString
Me.Invalidate()
最终使用
结束子
End Class
I need to determine the color of various points (pixels) on the form including graphic shapes (lines, circles, etc) that I add myself. This was quite easy in VB6 but seems to be very hard in vb.net 10.
I found the code snippet below on a google search. The CopyFromScreen function actually worked to copy a 1 pixel rectangle to point 0,0 and then read it''s color. But, when I try to replace Windows.Forms.Cursor.Postion with actual X,Y coordinates like (10,10) or even (me.location.x, me.location.y) I get an error message stating that integers can be used with CopyFromScreen.
Any ideas on why I cannot replace Windows.Forms.Cursor.Postion with numbers or some type of declared variable???
As always, thanks in advance for any help!!
Gary Vogel
Public Class Form1
Private WithEvents timer1 As New Windows.Forms.Timer
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
timer1.Interval = 10
timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles timer1.Tick
Using bmp As New Bitmap(1, 1)
Using g As Graphics = Graphics.FromImage(bmp)
g.CopyFromScreen(Windows.Forms.Cursor.Position, _
New Point(0, 0), New Size(1, 1))
End Using
Me.Text = bmp.GetPixel(0, 0).ToString
Me.Invalidate()
End Using
End Sub
End Class
推荐答案
Windows.Forms.Cursor.Position
的类型为Point,可用于定义您自己的点并将其作为参数传递.
Windows.Forms.Cursor.Position
is of type Point and can be used to define your own point and pass that as parameter.
System.Drawing.Point MyPosition = new Point(100, 100)
g.CopyFromScreen(MyPosition, New Point(0, 0), New Size(1, 1))
祝你好运!
Good luck!
这篇关于屏幕上点的颜色(像素)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!