Adjust Brightness with scrollbar

Found the following code to adjust brightness with a scroll bar but as soon as i slide the bar the picture turns black and is done.  How should i set the min max on the bar?  Right now it is at the default setting.  Have played with it to now avail.

    Private Sub sbbrightness_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles sbbrightness.Scroll
        Dim brightness As Single = CSng(sbbrightness.Value / 100) ' no change in brightness
        Dim contrast As Single = 1.0F ' no change in contrast

        Dim adjustedBrightness As Single = brightness - 1.0F
        'create matrix that will brighten and contrast the image
        Dim image_attr As New Imaging.ImageAttributes
        Dim cm As Imaging.ColorMatrix = New Imaging.ColorMatrix(New Single()() _
            { _
            New Single() {contrast, 0.0, 0.0, 0.0, 0.0}, _
            New Single() {0.0, contrast, 0.0, 0.0, 0.0}, _
            New Single() {0.0, 0.0, contrast, 0.0, 0.0}, _
            New Single() {0.0, 0.0, 0.0, 1.0, 0.0}, _
            New Single() {adjustedBrightness, adjustedBrightness, adjustedBrightness, 0.0, 1.0}})

        Dim rect As Rectangle = _
            Rectangle.Round(pbdigitalphotos.Image.GetBounds(GraphicsUnit.Pixel))
        Dim wid As Integer = pbdigitalphotos.Image.Width
        Dim hgt As Integer = pbdigitalphotos.Image.Height

        Dim img As New Bitmap(wid, hgt)
        Dim gr As Graphics = Graphics.FromImage(img)

        image_attr.SetColorMatrix(cm)
        gr.DrawImage(pbdigitalphotos.Image, rect, 0, 0, wid, _
            hgt, GraphicsUnit.Pixel, image_attr)

        pbdigitalphotos.Image = img
    End Sub

Open in new window

MillkindAsked:
Who is Participating?
 
ArkCommented:
Brightness should be -1 (black) to 0 (neutral) to 1 (white)
Imports System.Drawing.Imaging

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With HScrollBar1
            .Minimum = -100
            .Maximum = 100
            .Value = 0
        End With
    End Sub

    Private Sub HScrollBar1_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
        PictureBox1.Refresh()
    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        Dim brt As Single = HScrollBar1.Value / 100
        Dim image_attr As New ImageAttributes
        Dim cm As ColorMatrix = New ColorMatrix(New Single()() _
            { _
            New Single() {1.0, 0.0, 0.0, 0.0, 0.0}, _
            New Single() {0.0, 1.0, 0.0, 0.0, 0.0}, _
            New Single() {0.0, 0.0, 1.0, 0.0, 0.0}, _
            New Single() {0.0, 0.0, 0.0, 1.0, 0.0}, _
            New Single() {brt, brt, brt, 1.0, 1.0}})
        Dim rect As Rectangle = _
            Rectangle.Round(PictureBox1.Image.GetBounds(GraphicsUnit.Pixel))
        Dim wid As Integer = PictureBox1.Image.Width
        Dim hgt As Integer = PictureBox1.Image.Height

        image_attr.SetColorMatrix(cm)
        e.Graphics.DrawImage(PictureBox1.Image, rect, 0, 0, wid, _
            hgt, GraphicsUnit.Pixel, image_attr)

    End Sub

End Class

Open in new window

0
 
MillkindAuthor Commented:
Perfect, Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.