Option Explicit Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long,ByVal x As Long,ByVal y As Long,ByVal crColor As Long) As Long Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long,ByVal y As Long) As Long Private Declare Function GetTickCount Lib "kernel32" () As Long Private Sub Command1_Click() Dim c As Long Me.Caption = "正在翻转..." c = GetTickCount() If PictureInvHAPI(Picture1) = 1 Then Me.Caption = "翻转完成,用时 " & CStr(GetTickCount() - c) & " 毫秒!" End If End Sub Public Function PictureInvHAPI(pic As PictureBox) As Long Dim w As Integer,h As Integer Dim sm As Long,ar As Boolean Dim i As Long,j As Long Dim clr As Long sm = pic.ScaleMode ar = pic.AutoRedraw pic.ScaleMode = 3 pic.AutoRedraw = True w = pic.ScaleWidth - 1 h = pic.ScaleHeight - 1 For j = 0 To h For i = 0 To (w - (w Mod 2)) / 2 clr = GetPixel(pic.hdc,i,j) 'Debug.Print clr SetPixel pic.hdc,j,GetPixel(pic.hdc,w - i,j) SetPixel pic.hdc,clr Next i Next j pic.Refresh pic.ScaleMode = sm pic.AutoRedraw = ar PictureInvHAPI = &H1 End Function Public Function PictureInvH(pic As PictureBox) As Long Dim w As Integer,j As Long Dim clr As Long sm = pic.ScaleMode ar = pic.AutoRedraw pic.ScaleMode = 3 pic.AutoRedraw = True w = pic.ScaleWidth - 1 h = pic.ScaleHeight - 1 For j = 0 To h For i = 0 To (w - (w Mod 2)) / 2 clr = pic.Point(i,j) 'Debug.Print clr pic.PSet (i,j),pic.Point(w - i,j) pic.PSet (w - i,clr Next i Next j pic.ScaleMode = sm pic.AutoRedraw = ar PictureInvH = &H1 End Function
从上面的代码来测试,225X121的图片 3GHz AMD 双核cpu 4G内存,VB代码平均使用450ms 而 API使用350ms
加上hdc = GetDC(pic.hWnd) 和 ReleaseDC 两者竟然出奇的一致
也再一次说明VB不过用类封装了大量的WindowsAPI