I am using Pset to draw grid on my picture box every time my App. loads. But this take a lot of time to execute. Can anyone suggest me an alternate method of drawing grid on picture box?
Printable View
I am using Pset to draw grid on my picture box every time my App. loads. But this take a lot of time to execute. Can anyone suggest me an alternate method of drawing grid on picture box?
Use the SetPixel API call rather than the PSet function and you will see a significant increase in speed, the declare goes like this:
and implement it something like this:Code:
Declare Function SetPixel Lib "gdi32" Alias "SetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Good Luck :)Code:Private Sub Form_Paint()
dim x as Integer, y as Integer, color as Long
dim returnvalue as Long
x = 15 ' 15th pixel across
y = 15 ' 15th pixel down
color = RGB(0, 0, 255) ' blue
returnvalue = SetPixel(x, y, color) ' color pixel at x=15 y=15 the color blue
End Sub
Even Quicker !!!
Use API SetBitmapBits.
You can dump graphics straight out of a byte array...
I used it for a graphics program when the setpixel API was still too slow
Even, getbitmapbits, modify the array then setbitmap bits really flies
Hello YoungBuck.
I tried your code but It shows an error "Argument Not Optional". Itried to find out where the error was but could not get it. Please give me the proper code.
Thanks a lot.
Kinjal
Hi.
Code: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 Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Sub Form_Paint()
Dim winrect As RECT ' rectangle of window Form1
Dim rgbval As Long ' RGB value of the randomly selected color
Dim x As Long, y As Long ' counters for x and y coordinates
Dim retval As Long ' return value
' Get the rectangle of window Form1.
retval = GetWindowRect(Form1.hwnd, winrect)
' Loop through each pixel within Form1.
For y = 0 To winrect.Bottom - winrect.Top Step 15
For x = 0 To winrect.Right - winrect.Left Step 15
rgbval = RGB(0, 0, 255)
' Set the pixel to the color above.
retval = SetPixel(Form1.hdc, x, y, rgbval)
Next x
Next y
End Sub
Sorry Kinjal replace
with thisCode:returnvalue = SetPixel(x, y, color)
sorry for the typo, also if you are going to be drawing to a picturebox you may want to use the Picturebox's paint event (Picture1_Paint) and the Picturebox's handle (Picture1.hdc).Code:returnvalue = SetPixel(Form1.hdc, x, y, color)
Sorry :P