PDA

Click to See Complete Forum and Search --> : getpixel api


Mushroom Realm
Jul 6th, 2002, 06:56 PM
Im used this code and it didnt work. im trying to invert the image

Public Sub mnuInvert_Click()
Dim Pixels() As RGBTriplet
Dim X As Integer
Dim Y As Integer
Dim Bits As Integer
GetPixel Picture1, X, Y
For X = 0 To Picture1.ScaleWidth
For Y = 0 To Picture1.ScaleHeight
With Pixels(X, Y)
.rgbRed = 255 - .rgbRed
.rgbGreen = 255 - .rgbGreen
.rgbBlue = 255 - .rgbBlue
End With
Next Y
Next X
SetPixel Picture1, X, Y, Bits
Picture1.Picture = Picture1.Image
End Sub
does anyone know why?

Rick Bull
Jul 7th, 2002, 06:16 AM
I think it's because you are putting the pset outside of the loops, plus I think you need to use RGB on the Bits variable, don't you? But there is a much quicker easier way if you're interested, try using this API call:


Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Declare Function InvertRect Lib "user32" Alias "InvertRect" (ByVal hdc As Long, lpRect As RECT) As Long

RuneLancer
Jul 7th, 2002, 05:42 PM
If you really want to stick to the GetPixel and SetPixel commands, try this bit of code...

for i = 0 to Picture1.ScaleWidth
for j = 0 to Picture1.ScaleHeight
c = GetPixel(Picture1.hDC, i, j)
c = &HFFFFFF - c
SetPixel(Picture1.hDC, i, j, c)
next
next

Simple as that. You're simply going over every single pixel in the picturebox control, getting their color, calculating the inverted color, and plotting them back. This is considerably slow though. Pixel by pixel plotting takes a rather long time compared to simply using the InvertRect API call.