Visual Basic

Sunday, November 2, 2008

Change Color Of Pixel In PictureBox


'Add a module to your project (In the menu choose Project -> Add Module, Then click Open)
'Add 1 CommandButton (named Command1) to your form, And 1 PictureBox
'(Named Picture1). If you want You can put picture in the picturebox. Set the
'PictureBox AutoRedraw property to True.
'Insert this code to the module :

Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, _
ByVal Y As Long, ByVal crColor As Long) As Long

'Insert the following code to your form:

Private Sub Command1_Click()
Dim s As Long
'Replace the '10,10' below with the position of the pixel you want to change its color.
'Replace the RGB(255, 0, 0) with the color you want to assign to this pixel.
'RGB(255, 0, 0) is red color.
s = SetPixel(Picture1.hdc, 10, 10, RGB(255, 0, 0))
Picture1.Refresh
End Sub