jmasterx
jmasterx

Reputation: 54123

Brightness/exposure function with vb .net

I'm making a simple image editor in VB.NET, and one of the functions is brightness/ exposure.

this is how I'm doing it:

For i = 0 To img.Width - 1
    For j = 0 To img.Height - 1
        Dim s As Color = img.GetPixel(i, j)

        Dim r As Integer = s.R * 2
        Dim g As Integer = s.G * 2
        Dim b As Integer = s.B * 2
        If s.R * 2 > 255 Then
            r = 255
        End If

        If s.G * 2 > 255 Then
            g = 255
        End If
        If s.B * 2 > 255 Then
            b = 255
        End If
        Dim x As Color = Color.FromArgb(255, r, g, b)
        img.SetPixel(i, j, x)

    Next
Next

where 2 is brightness which makes it twice as bright.

Only problem is this doesnt seem to work well because it does it, but takes about 30 seconds! What am I doing wrong? is there a better way to implement it?

Thanks

Upvotes: 4

Views: 12483

Answers (2)

xpda
xpda

Reputation: 15813

You can use a ColorMatrix and ImageAttributes to do it a lot faster. Here's how:

Imports System.Drawing.Imaging

Public Class Form1

Dim g As Graphics
Dim img As Image
Dim r As Rectangle

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  ' load photo file into picture box and initialize graphics
  img = Image.FromFile("c:\tmp.jpg")
  PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height, PixelFormat.Format32bppArgb)
  g = Graphics.FromImage(PictureBox1.Image)
  r = New Rectangle(0, 0, PictureBox1.Width, PictureBox1.Height)
  g.DrawImage(img, r)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Call setBrightness(0.2)
End Sub

Sub setBrightness(ByVal Brightness As Single)
  ' Brightness should be -1 (black) to 0 (neutral) to 1 (white)

  Dim colorMatrixVal 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() {Brightness, Brightness, Brightness, 0, 1}}

  Dim colorMatrix As New ColorMatrix(colorMatrixVal)
  Dim ia As New ImageAttributes

  ia.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)

  g.DrawImage(img, r, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia)
  PictureBox1.Refresh()
End Sub

End Class

Upvotes: 2

John Boker
John Boker

Reputation: 83709

Using the following examples it should be pretty easy to put together a project that changes the brightness/contrast and other features of images.

here's is an example of using a ColorMatrix to be able to change an image, it should be much faster:

http://www.developerfusion.com/forum/thread/35125/

and here is an example of doing brightness and contrast at the same time:

http://www.codeproject.com/KB/graphics/multiple_color_matrices.aspx

another example of using a ColorMatrix:

http://www.codeproject.com/KB/GDI-plus/colormatrix.aspx

and for reference on ColorMatrix:

http://msdn.microsoft.com/en-us/library/ms534063%28VS.85%29.aspx

Upvotes: 1

Related Questions