[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 566
  • Last Modified:

Adjust Brightness of image

I am so close to having a brightness adjuster that works with the system the way i want it to.

I have a scroll-bar from -100 to 100.
I have a pboriginal with the original image that should never change.
I have a pbaltered with the altered image that is to be changed.

I call the function with this:
       Try
            pbaltered.Image = BrightnessFilter(sbbrightness.Value, pboriginal.Image)
        Catch ex As Exception
            '' MsgBox(ex.ToString)
        End Try

Open in new window


Here is the function:

    Public Shared Function BrightnessFilter(ByVal BrightnessValue As Integer, ByRef b As System.Drawing.Image) As Bitmap
        Dim newBitmap As New System.Drawing.Bitmap(b)
        Dim converted As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(b)
        Dim imageAttributes As New Imaging.ImageAttributes()
        Dim width As Integer = b.Width
        Dim height As Integer = b.Height

        Dim colorMatrixElements As Single()() = { _
          New Single() {1, 0, 0, 0, 0}, _
          New Single() {0, 1, 0, 0, 0}, _
          New Single() {0, 0, 1, 0, 0}, _
          New Single() {0, 0, 0, 1, 0}, _
          New Single() {BrightnessValue / 100, BrightnessValue / 100, BrightnessValue / 100, 0, 1}}
        Dim colorMatrix As New Imaging.ColorMatrix(colorMatrixElements)
        imageAttributes.SetColorMatrix(colorMatrix, Imaging.ColorMatrixFlag.Default, Imaging.ColorAdjustType.Bitmap)
        ' overdraw the bitmap with brightened colors
        converted.DrawImage(newBitmap, New Rectangle(0, 0, newBitmap.Width, newBitmap.Height), 0, 0, newBitmap.Width, newBitmap.Height, GraphicsUnit.Pixel, imageAttributes)
        converted.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        Return newBitmap
    End Function

Open in new window


The problem is two fold, it not only alters pbaltered but pboriginal.  Also the saturation seems very off.  It goes completely black or white after only a few clicks. I think that is due to pboriginal getting changed.
0
Millkind
Asked:
Millkind
  • 9
  • 8
  • 2
2 Solutions
 
CodeCruiserCommented:
What happens if you change

pbaltered.Image = BrightnessFilter(sbbrightness.Value, pboriginal.Image)

to

pbaltered.Image = BrightnessFilter(sbbrightness.Value, pboriginal.Image.Clone())
0
 
MillkindAuthor Commented:
That actually broke it.  Neither pictures change with that.
0
 
CodeCruiserCommented:
That is strange. The reason it was changing both original and altered images is that the image is being passed as a reference. So  I cloned it so that original does not get altered.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
MillkindAuthor Commented:
Could it be the event im using to call it?

    Private Sub ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles sbbrightness.ValueChanged, sbZoom.ValueChanged
        Try
            pbaltered.Image = BrightnessFilter(sbbrightness.Value, pboriginal.Image)
        Catch ex As Exception
            '' MsgBox(ex.ToString)
        End Try

        Try
            pbaltered.Image = Zoom(pboriginal.Image, sbZoom.Value)
        Catch ex As Exception
            '' MsgBox(ex.ToString)
        End Try
    End Sub

Open in new window

0
 
CodeCruiserCommented:
No that should not be a problem. Also change the Zoom one to send a cloned image.
0
 
MillkindAuthor Commented:
Zoom works fine with and  without the clone.

    Public Function Zoom(ByVal img As System.Drawing.Image, ByVal ZoomValue As Int32) As System.Drawing.Image
        Dim width As Int32 = Convert.ToInt32(img.Width * ZoomValue) / 100
        Dim height As Int32 = Convert.ToInt32(img.Height * ZoomValue) / 100
        'Create a new image based on the zoom parameters we require
        Dim zoomImage As New System.Drawing.Bitmap(img, width, height)

        'Create a new graphics object based on the new image
        Dim converted As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(ZoomImage)

        'Clean up the image
        converted.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic

        'Return the zoomed image
        Return ZoomImage
    End Function

Open in new window

0
 
CodeCruiserCommented:
Ah I know what's the issue. You are doing brightness first and then zoom and brightness is being overwritten by zoom

Change above code to

    Private Sub ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles sbbrightness.ValueChanged, sbZoom.ValueChanged
        Dim img As Image
        Try
            img = BrightnessFilter(sbbrightness.Value, pboriginal.Image.Clone)
        Catch ex As Exception
            '' MsgBox(ex.ToString)
        End Try

        Try
            img = Zoom(img, sbZoom.Value)
        Catch ex As Exception
            '' MsgBox(ex.ToString)
        End Try
        pbAltered.Image = img
    End Sub

Open in new window

0
 
MillkindAuthor Commented:
Copier that and over wrote the other code.  Zoom still works brightness does nothing.
0
 
CodeCruiserCommented:
What happens if you comment out the zoom code?
0
 
MillkindAuthor Commented:
Then zoom doesn't work. HA, seriously The brightness still doesn't work.  It does nothing now.
0
 
CodeCruiserCommented:
Then something is wrong in that function probably. Step through the code to make sure correct values are being passed around and code is executing as expected especially since you catch block is not showing you the exception.
0
 
MillkindAuthor Commented:
When i turn the catch on there is no error thrown.  It goes through the code with no error.
I put back in the follwoing code and it is back to doing something but not what it should do.  Both the original and altered get changed and go way to fast.

 
  Private Sub ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles sbbrightness.ValueChanged, sbZoom.ValueChanged
        Try
            pbaltered.Image = BrightnessFilter(sbbrightness.Value, pboriginal.Image)
        Catch ex As Exception
            '' MsgBox(ex.ToString)
        End Try

        Try
            pbaltered.Image = Zoom(pboriginal.Image, sbZoom.Value)
        Catch ex As Exception
            '' MsgBox(ex.ToString)
        End Try
    End Sub

Open in new window

0
 
CodeCruiserCommented:
Change above to

  Private Sub ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles sbbrightness.ValueChanged, sbZoom.ValueChanged
        Dim img As Image
        If Not Isnothing(pbAltered.Image) Then
           img = pbAltered.Image
        Else
           img = pbOriginal.Image.Clone()
        End If
        Try
            img = BrightnessFilter(sbbrightness.Value, img)
        Catch ex As Exception
            '' MsgBox(ex.ToString)
        End Try

        Try
            img = Zoom(img, sbZoom.Value)
        Catch ex As Exception
            '' MsgBox(ex.ToString)
        End Try
        pbAltered.Image = img
    End Sub

Open in new window

0
 
MillkindAuthor Commented:
Changed it to the following code and brightness doesn't work.  Zoom still works but is very slow.  And throws no errors

        Dim img As Image
        Try
            If Not IsNothing(pbaltered.Image) Then
                img = pbaltered.Image
            Else
                img = pboriginal.Image.Clone()
            End If

            img = BrightnessFilter(sbbrightness.Value, img)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        Try
            img = Zoom(img, sbZoom.Value)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        pbaltered.Image = img

Open in new window

0
 
ArkCommented:
    Private Sub hsbBrightness_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles hsbBrightness.ValueChanged
        pbAltered.Image = BrightnessFilter(hsbBrightness.Value, pbOriginal.Image)
    End Sub
    Public Shared Function BrightnessFilter(ByVal BrightnessValue As Integer, ByVal b As System.Drawing.Image) As Bitmap
        Dim newBitmap As New System.Drawing.Bitmap(b.Width, b.Height)
        Dim converted As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(newBitmap)
        Dim imageAttributes As New Imaging.ImageAttributes()
        Dim width As Integer = b.Width
        Dim height As Integer = b.Height

        Dim colorMatrixElements As Single()() = { _
          New Single() {1, 0, 0, 0, 0}, _
          New Single() {0, 1, 0, 0, 0}, _
          New Single() {0, 0, 1, 0, 0}, _
          New Single() {0, 0, 0, 1, 0}, _
          New Single() {BrightnessValue / 100, BrightnessValue / 100, BrightnessValue / 100, 0, 1}}
        Dim colorMatrix As New Imaging.ColorMatrix(colorMatrixElements)
        imageAttributes.SetColorMatrix(colorMatrix, Imaging.ColorMatrixFlag.Default, Imaging.ColorAdjustType.Bitmap)
        ' overdraw the bitmap with brightened colors
        converted.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        converted.DrawImage(b, New Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, imageAttributes)
        Return newBitmap
    End Function

Open in new window

0
 
MillkindAuthor Commented:
Ark, That looking good i have to work with it some more before i award points though.
0
 
MillkindAuthor Commented:
Had to make a few changes to how the subs do the scroll bars, had to add a third picture box.
Go from original to contrasted to altered.  Contrast affects contrasted and zoom works from only the contrasted picture box.  Works great now. I would like to split the points 50/50 Because Ark gave me great code but CodeCruiser suggestion of spliting the subs was part of the problem too.  Is this acceptable to both parties?
0
 
CodeCruiserCommented:
Ok with me.
0
 
ArkCommented:
No problem
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 9
  • 8
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now