Visual Basic Bitmap Routines
SetPixelV: Manipulate Picture Box Image Brightness
Posted:   Tuesday January 16, 2001
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows 2000
OS restrictions:   None
Author:   VBnet, Tanner Helland

Here's an interesting bit of code by Tanner Helland that takes any image assigned to a picturebox and rapidly adjusts the image brightness. As the new image is displayed, it progressively wipes overtop the old image (the demo picture shows the progress half-way across the girl). This code could be easily added (and would make a nice addition) to the child's drawing canvas demo at ExtFloodFill: Fill Polygonal Regions.
 BAS Module Code

 Form Code
Add a picture box (Picture1, a command button (Command1) and a textbox to the form. Set the picture box ScaleMode to 3-Pixels, and AutoRedraw to True. Add the following code to the form:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Declare Function SetPixelV Lib "gdi32" _
  (ByVal hDC As Long, _
   ByVal x As Long, _
   ByVal y As Long, _
   ByVal crColor As Long) As Byte
Private Declare Function GetPixel Lib "gdi32" _
  (ByVal hDC As Long, _
   ByVal x As Long, _
   ByVal y As Long) As Long

Private Sub Command1_Click()

  'variables for brightness, colour calculation, positioning
   Dim Brightness As Single
   Dim NewColour As Long
   Dim pixHdc As Long   
   Dim x As Long, y As Long
   Dim r As Integer, g As Integer, b As Integer
  'change the brightness to a percent
   Brightness = CSng(Val(Text1.Text) / 100)
   pixHdc = Picture1.hDC
  'run a loop through the picture to change every pixel
   For x = 0 To Picture1.ScaleWidth
      For y = 0 To Picture1.ScaleHeight
        'get the current colour value
         NewColour = GetPixel(pixHdc , x, y)
        'extract the R,G,B values from the long returned by GetPixel
         r = (NewColour Mod 256)
         b = (Int(NewColour \ 65536))
         g = ((NewColour - (b * 65536) - r) \ 256)
        'change the RGB settings to their appropriate brightness
         r = r * Brightness
         b = b * Brightness
         g = g * Brightness
        'make sure the new variables aren't too high or too low
         If r > 255 Then r = 255
         If r < 0 Then r = 0
         If b > 255 Then b = 255
         If b < 0 Then b = 0
         If g > 255 Then g = 255
         If g < 0 Then g = 0
        'set the new pixel
         SetPixelV pixHdc, x, y, RGB(r, g, b)
     'continue through the loop
      Next y
     'refresh the picture box
     '(a nice visual progress effect)
   Next x
  'final picture refresh
End Sub
If you're using a slower machine, you may want to reduce the number of times the picture box updates by using this line instead of the Refresh line above:

   If x MOD 10 Then Picture1.Refresh


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy



Hit Counter