Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleBrighten or darken a picture
Keywordspicture, brighten, darken, brightness, image
CategoriesGraphics
 
Use GetDIBits to load the picture's pixel values. Adjust the color components so they are closeer to 0 (darker) or 255 (brighter). Then use SetDIBits to save the result.
 
Private Sub SetBrightness(ByVal pic1 As PictureBox, ByVal _
    pic2 As PictureBox, ByVal brightness As Single)
Dim bitmap_info As BITMAPINFO
Dim pixels() As Byte
Dim bytes_per_scanLine As Integer
Dim pad_per_scanLine As Integer
Dim X As Integer
Dim Y As Integer
Dim fraction As Single

    ' Get the original picture's bits.
    ' Prepare the bitmap description.
    With bitmap_info.bmiHeader
        .biSize = 40
        .biWidth = Picture1.ScaleWidth
        ' Use negative height to scan top-down.
        .biHeight = -Picture1.ScaleHeight
        .biPlanes = 1
        .biBitCount = 32
        .biCompression = BI_RGB
        bytes_per_scanLine = ((((.biWidth * .biBitCount) + _
            31) \ 32) * 4)
        pad_per_scanLine = bytes_per_scanLine - (((.biWidth _
            * .biBitCount) + 7) \ 8)
        .biSizeImage = bytes_per_scanLine * Abs(.biHeight)
    End With

    ' Load the bitmap's data.
    ReDim pixels(1 To 4, 1 To Picture1.ScaleWidth, 1 To _
        Picture1.ScaleHeight)
    GetDIBits Picture1.hdc, Picture1.Image, _
        0, Picture1.ScaleHeight, pixels(1, 1, 1), _
        bitmap_info, DIB_RGB_COLORS

    ' Modify the pixels.
    If brightness < 0 Then
        ' Darken.
        fraction = (100 + brightness) / 100
        For Y = 1 To pic1.ScaleHeight
            For X = 1 To pic1.ScaleWidth
                pixels(pixR, X, Y) = fraction * _
                    pixels(pixR, X, Y)
                pixels(pixG, X, Y) = fraction * _
                    pixels(pixG, X, Y)
                pixels(pixB, X, Y) = fraction * _
                    pixels(pixB, X, Y)
            Next X
        Next Y
    Else
        ' Brighten.
        fraction = brightness / 100
        For Y = 1 To pic1.ScaleHeight
            For X = 1 To pic1.ScaleWidth
                pixels(pixR, X, Y) = pixels(pixR, X, Y) + _
                    fraction * (255 - pixels(pixR, X, Y))
                pixels(pixG, X, Y) = pixels(pixG, X, Y) + _
                    fraction * (255 - pixels(pixG, X, Y))
                pixels(pixB, X, Y) = pixels(pixB, X, Y) + _
                    fraction * (255 - pixels(pixB, X, Y))
            Next X
        Next Y
    End If

    ' Display the result.
    SetDIBits Picture2.hdc, Picture2.Image, _
        0, Picture1.ScaleHeight, pixels(1, 1, 1), _
        bitmap_info, DIB_RGB_COLORS
    Picture2.Picture = Picture2.Image
End Sub
 

For more information on image processing, see my book Visual Basic Graphics Programming.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated