Having an image like this (original size increased by 100%):
Under C# or else Vb.Net, I wonder if I could use a color matrix or other workaround to increase the brightness/luminosity only to the white/greyed color of that image keeping the black background color untouched?
In other words, I would like to make the white cross brighter (only the cross, the white/grey pixels).
This is the brightness result that I expect to achieve:
Both images are taken from a 3rd-party application, the first image I shown is from a not focused close-button and the second image is the same button focused, the black portion of the image is the background of that 3rd-party app
The thing is that for personal reasons in my own application I'm using that image as a close-button and the same background color then I just wondered to simulate a button-focus in that way by giving brightness to the "X" in that image, I need to do the same with other images.
My knowledges of image processing are very poor, I've just been looking to approachs like this below, but I cannot get the expected result because the brightness increases in the entire image:
Adjust brightness contrast and gamma of an image
Update
I'm trying to use @Paul Ishak approach.
I have the image set on a picturebox, the idea is to increase the brightness when the user is hovering the control, and reset the image when the mouse leaves,
the second time that I try to adjust the brightness it throws an ArgumentException at this line:
bm = New Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
with this message:
A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll
This is the code that I'm using, what is wrong?:
Private minimizeBitmap As Bitmap
Private Sub PictureBox_MinimizeButton_MouseEnter(ByVal sender As Object, ByVal e As EventArgs) _
Handles PictureBox_MinimizeButton.MouseEnter
Dim pcb As PictureBox = DirectCast(sender, PictureBox)
If Me.minimizeBitmap Is Nothing Then
Me.minimizeBitmap = New Bitmap(pcb.ClientRectangle.Width, pcb.ClientRectangle.Height)
pcb.DrawToBitmap(Me.minimizeBitmap, pcb.ClientRectangle)
End If
pcb.BackgroundImage = BrightenPixels(Me.minimizeBitmap, threshold:=33, modTimes:=5D)
End Sub
Private Sub PictureBox_MinimizeButton_MouseLeave(ByVal sender As Object, ByVal e As EventArgs) _
Handles PictureBox_MinimizeButton.MouseLeave
If Me.minimizeBitmap IsNot Nothing Then
Me.minimizeBitmap.Dispose()
End If
DirectCast(sender, PictureBox).BackgroundImage = Nothing
End Sub
Public Function BrightenPixels(ByVal image As Bitmap, threshold As Decimal, modTimes As Decimal) As Bitmap
Dim bmp As Bitmap
modTimes = Math.Abs(modTimes)
If image Is Nothing Then
Throw New ArgumentNullException(paramname:="image")
End If
Try
bmp = New Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Catch ex As Exception
MsgBox(ex.GetType.Name)
MsgBox(ex.Message)
End Try
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawImage(image, New Point(0, 0))
Dim rect As New Rectangle(New Point(0, 0), bmp.Size)
Dim bitmapData As System.Drawing.Imaging.BitmapData = bmp.LockBits(rect, Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim ptr As IntPtr = bitmapData.Scan0
Dim byteCount As Integer = bitmapData.Stride * bitmapData.Height
Dim argb(byteCount - 1) As Byte
Marshal.Copy(ptr, argb, 0, byteCount)
For i As Integer = 0 To byteCount - 1 Step 4
Dim red As Decimal = argb(i + 2)
Dim green As Decimal = argb(i + 1)
Dim blue As Decimal = argb(i)
Dim percentR As Decimal = (red / 255) * 100
Dim percentG As Decimal = (green / 255) * 100
Dim percentB As Decimal = (blue / 255) * 100
Dim aboveThresholdCount As Integer = 0
If percentR > threshold Then aboveThresholdCount += 1
If percentG > threshold Then aboveThresholdCount += 1
If percentB > threshold Then aboveThresholdCount += 1
' Label3.Text = aboveThresholdCount.ToString
If aboveThresholdCount = 3 Then
red = red * modTimes
green = green * modTimes
blue = blue * modTimes
If red > 255 Then red = 255
If green > 255 Then green = 255
If blue > 255 Then blue = 255
End If
argb(i + 2) = CByte(Math.Round(red, 0))
argb(i + 1) = CByte(Math.Round(green, 0))
argb(i) = CByte(Math.Round(blue, 0))
Next
Marshal.Copy(argb, 0, ptr, byteCount)
bmp.UnlockBits(bitmapData)
Return bmp
End Function