求解getdc获取设备场景句柄
Option Explicit
Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Dim a&, b&, c&, d&, x&, y&
Private Sub Command1_Click()
For x = 0 To Picture1.ScaleHeight - 1
For y = 0 To Picture1.ScaleWidth - 1
a = GetPixel(GetDC(Picture1), y , x )
b = a And &HFF
c = (a And 65280) \ 256
d = (a And &HFF0000) \ 65536
SetPixel GetDC(Picture1), y , x , RGB(b, b, b)
Next y
Next x
Print a
Print b
Print c
Print d
Print GetDC(Picture1.hdc)
End Sub
因我是初学者,所以以上代码是模仿别人,自己修改的,目的是保留位图的某个通道。问题是getdc这个函数获取设备场景总是失败,还有就是getpixel函数取的是逻辑坐标,那么程序中的表达式是否正确?
Print a
Print b
Print c
Print d
Print GetDC(Picture1.hdc)
这几行其实没用,为的就是看是否取到了像素的颜色和设备场景句柄。
希望有高手能指点关于设备场景和逻辑坐标的关键点。
[解决办法]
另外楼主竟然拿
GetDC(Picture1.hdc)
这种代码来问,说明你还是没理解
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
里的hwnd指的是WINDOWS窗口句柄哦,连类型都没搞对啊!
建议你把
SetPixel GetDC(Picture1), y , x , RGB(b, b, b)
这类改为
SetPixel Picture1.hDc, y , x , RGB(b, b, b)
试下
[解决办法]
1:a = GetPixel(GetDC(Picture1), y , x )
GetDC(Picture1)=GetDC(0) 取得整个屏幕的DC a的值为屏幕固定坐标的值,而不是Pic上的。
2:SetPixel GetDC(Picture1), y , x , RGB(b, b, b)
同1
3:Print GetDC(Picture1.hdc)
GetDC(Picture1.hwnd)=picture1.hdc picture1的DC