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
 
 
 
 
 
TitleUse an ImageAttributes object to gamma correct an image in Visual Basic .NET
DescriptionThis example shows how to use an ImageAttributes object to gamma correct an image in Visual Basic .NET
Keywordscolor, color components, gamm, gamm correction, ColorMatrix, ImageAttributes, VB .NET
CategoriesVB.NET, Graphics
 
Gamma correction is a process of adjusting an image's brightness and color balance to produce an accurate and pleasing result on the screen. Look here for more details.

The ImageAttributes object can perform gamma correction. The following code makes an ImageAttributes object and uses its SetGamma method to set a gamma value. It then uses a Graphics object's DrawImage method to draw the original image, passing it the ImageAttributes object to set the image's gamma value.

When you change the program's scroll bar or enter a new value in the TextBox, the program uses this routine to adjust the image's gamma accordingly. The result is quite impressive. The original image is as I scanned it from a photograph. Setting gamma to 0.4 makes the image much brighter and sharper.

 
Private Sub picResult_Paint(ByVal sender As Object, ByVal e _
    As System.Windows.Forms.PaintEventArgs) Handles _
    picResult.Paint
    Dim pts() As Point = { _
        New Point(0, 0), _
        New Point(picSource.Image.Width - 1, 0), _
        New Point(0, picSource.Image.Height - 1) _
    }
    Dim rect As New Rectangle(0, 0, picSource.Image.Width, _
        picSource.Image.Height)
    Dim image_attr As New ImageAttributes
    Dim gamma As Single = hbarGamma.Value / 10
    image_attr.SetGamma(gamma)
    e.Graphics.DrawImage(picSource.Image, pts, rect, _
        GraphicsUnit.Pixel, image_attr)
End Sub
 
This method is extremely fast so you can easily adjust gamma in real time.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated