加入一个TextBox控件,一个Command控件
成都创新互联公司专注于企业网络营销推广、网站重做改版、宁安网站定制设计、自适应品牌网站建设、HTML5、商城网站开发、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为宁安等各大城市提供网站开发制作服务。
代码:
Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As Long
Private Sub Command1_Click()
Dim Color As Long
WindowDC = GetWindowDC(0) '获取屏幕的设备场景
Color = GetPixel(WindowDC, 500, 100) '获指定点的颜色
'分解RGB颜色值
R = (Color Mod 256) '红色
b = (Int(Color \ 65536)) '蓝色
G = ((Color - (b * 65536) - R) \ 256) '绿色
Text1.BackColor = RGB(R, G, b)
End Sub
要获取指定点的颜色,还是要调用WIN的API来实现Private Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
如获取当前窗口指定座标点的(x=100,y=100),这里的x,y是指像素点,不是VB默认的度量单位(缇)颜色值=GetPixel(Me.hDc,100,100)
Using b As New Bitmap(1, 1)
Using g As Graphics = Graphics.FromImage(b)
g.CopyFromScreen(New Point(300, 300), New Point, New Size(1, 1))
PictureBox1.BackColor = b.GetPixel(0, 0)
End Using
End Using
复制当前屏幕左上角位置(300,300)的一个点,取其颜色
我有个笨办法,先用API抓图到内存里,然后再在根据你点鼠标的屏幕工作区坐标,去那图里取色。
-----------------------
'抓图所需的API
Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Integer) As Integer
Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer
Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer
Private Declare Function BitBlt Lib "GDI32" (ByVal srchDC As Integer, ByVal srcX As Integer, ByVal srcY As Integer, ByVal srcW As Integer, ByVal srcH As Integer, ByVal desthDC As Integer, ByVal destX As Integer, ByVal destY As Integer, ByVal op As Integer) As Integer
Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Integer) As Integer
Private Declare Function DeleteObject Lib "GDI32" (ByVal hObj As Integer) As Integer
Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) As Integer
Const SRCCOPY As Integer = HCC0020
'抓图的部分
Dim hDC, hMDC As Integer
Dim hBMP, hBMPOld As Integer
Dim sw, sh As Integer
hDC = GetDC(0)
hMDC = CreateCompatibleDC(hDC)
sw = Screen.PrimaryScreen.Bounds.Width
sh = Screen.PrimaryScreen.Bounds.Height
hBMP = CreateCompatibleBitmap(hDC, sw, sh)
hBMPOld = SelectObject(hMDC, hBMP)
BitBlt(hMDC, 0, 0, sw, sh, hDC, 0, 0, SRCCOPY)
hBMP = SelectObject(hMDC, hBMPOld)
Dim bmp As Bitmap = Image.FromHbitmap(New IntPtr(hBMP))
DeleteDC(hDC)
DeleteDC(hMDC)
DeleteObject(hBMP)
......
'取点的颜色
bmp.GetPixel(e.X, e.Y)
----------------------------
关键就是这些你自己组合吧,你分给的太少了,很麻烦,恕我不帮你改全了。如果要仔细帮你改,请另开高分贴,不要用新马甲来
要使用GetPixel函数来取得像素的颜色值,代码如下:
1
2
3
4
5
private void button1_Click(object sender, EventArgs e)
{
Color color = new Bitmap(pictureBox1.Image).GetPixel(10, 10);
MessageBox.Show(color.ToString());