Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
Dim bmp As New Bitmap("1.jpg")
Dim t As Single
t = Microsoft.VisualBasic.DateAndTime.Timer
If e.Button = MouseButtons.Left Then
PictureBox1.Image = InvertBitmap(bmp)
Else
PictureBox1.Image = InvertBitmap2(bmp)
End If
t = Microsoft.VisualBasic.DateAndTime.Timer - t
Me.Text = "耗时:" & t
End Sub
Private Function InvertBitmap(ByVal pBitmap As Bitmap) As Bitmap
Dim width As Integer = pBitmap.Width
Dim height As Integer = pBitmap.Height
Dim rect As Rectangle = New Rectangle(0, 0, width, height)
Dim bitmapData As BitmapData = pBitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb)
Dim stride As Integer = bitmapData.Stride
Dim RawData As IntPtr = bitmapData.Scan0
Dim i As Integer
Dim l As Integer = height * stride - 1
Dim b As Byte
Dim m As Byte = 255
For i = 0 To l
b = Marshal.ReadByte(RawData, i)
Marshal.WriteByte(RawData, i, m - b)
Next
pBitmap.UnlockBits(bitmapData)
Return pBitmap
End Function
Private Function InvertBitmap2(ByVal pBitmap As Bitmap) As Bitmap
Dim width As Integer = pBitmap.Width
Dim height As Integer = pBitmap.Height
Dim rect As Rectangle = New Rectangle(0, 0, width, height)
Dim bitmapData As BitmapData = pBitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb)
Dim stride As Integer = bitmapData.Stride
Dim RawData As IntPtr = bitmapData.Scan0
Dim i As Integer
Dim l As Integer = height * stride - 1
Dim b(l) As Byte
Dim m As Byte = 255
Marshal.Copy(RawData, b, 0, l)
For i = 0 To l
b(i) = m - b(i)
Next
Marshal.Copy(b, 0, RawData, l)
Dim g As Graphics = Drawing.Graphics.FromImage(bmp)
g.DrawImage(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia)
Private Function InvertBitmap(ByVal pBitmap As Bitmap) As Boolean
Dim width As Integer = pBitmap.Width
Dim height As Integer = pBitmap.Height
Dim rect As Rectangle = New Rectangle(0, 0, width, height)
Dim bitmapData As BitmapData = pBitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb)
Dim stride As Integer = bitmapData.Stride
Dim RawData As IntPtr = bitmapData.Scan0
Dim x, y As Integer
For y = 0 To height
For x = 0 To width
Next
Next
pBitmap.UnlockBits(bitmapData)
Return True
End Function