亮度
Dim degree As Integer = 50 '-255 到 255
Dim Width As Integer, Height As Integer
Dim Pic As Bitmap = New Bitmap(getimage("d:\test.jpg"))
Width = Pic.Width
Height = Pic.Height
Dim rect As New Rectangle(0, 0, Width, Height)
Dim bmpData As BitmapData = Pic.LockBits(rect, ImageLockMode.ReadWrite, Pic.PixelFormat)
Dim ptr As IntPtr = bmpData.Scan0
Dim offset As Integer = bmpData.Stride - bmpData.Width * 4
For j As Integer = 0 To Height - 1
For i As Integer = 0 To Width - 1
For k As Integer = 0 To 2
Dim pix As Integer = Marshal.ReadByte(ptr, k)
pix = pix + degree
If pix < 0 Then pix = Math.Max(0, pix)
If pix > 0 Then pix = Math.Min(255, pix)
Marshal.WriteByte(ptr, k, cByte(pix))
Next
ptr = CType(ptr.ToInt32 + 4, IntPtr)
Next
ptr = CType(ptr.ToInt32 + offset, IntPtr)
Next
Pic.UnlockBits(bmpData)
pic.Save("d:\test3.jpg")
----------------
对比度
Dim degree As Integer = -20 '-100 到 100
Dim contrast As Double = (100 + degree) / 100.0
contrast *= contrast
Dim Width As Integer, Height As Integer
Dim Pic As Bitmap = New Bitmap(getimage("d:\test.jpg"))
Width = Pic.Width
Height = Pic.Height
Dim rect As New Rectangle(0, 0, Width, Height)
Dim bmpData As BitmapData = Pic.LockBits(rect, ImageLockMode.ReadWrite, Pic.PixelFormat)
Dim ptr As IntPtr = bmpData.Scan0
Dim offset As Integer = bmpData.Stride - bmpData.Width * 4
For j As Integer = 0 To Height - 1
For i As Integer = 0 To Width - 1
For k As Integer = 0 To 2
Dim pix As Integer = Marshal.ReadByte(ptr, k)
pix = ((pix / 255 - 0.5) * contrast + 0.5) * 255
If pix < 0 Then pix = Math.Max(0, pix)
If pix > 0 Then pix = Math.Min(255, pix)
Marshal.WriteByte(ptr, k, cByte(pix))
Next
ptr = CType(ptr.ToInt32 + 4, IntPtr)
Next
ptr = CType(ptr.ToInt32 + offset, IntPtr)
Next
Pic.UnlockBits(bmpData)
pic.Save("d:\test3.jpg")