在VB里使用图形框打开两个图形。 首先,使用GetBitmapBits将原来的空白圆形存储到一个Byte数组中,圆形最好为正圆形。 然后,使用同样的API函数将修改后的圆形(包括用户绘的点线)存储到另一个Byte数组中。 最后,进行比较。以每4个Byte一组循环比较两个数组(4个Byte表示一个象素,可以使用MemoryCopy转换成Long),如果两组相同,说明是原来的图形,不予记录,如果不同,则根据圆形的宽度计算行坐标,圆形的高度计算列坐标,假设空白圆形的高和宽均为10,这时图形数组应该是10*10*4个Byte,行坐标即为数组索引index*10,列坐标为数组索引index mod 10。
错了,行坐标应为index/10的整数部分。
Option Explicit Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As LongPrivate Const ARRAY_SIZE As Long = 129600 '360*360*4Private Sub Command1_Click() Dim i As Long Dim lpOldBits(1 To ARRAY_SIZE) As Long Dim lpNewBits(1 To ARRAY_SIZE) As Long
For i = 1 To ARRAY_SIZE Step 4 If lpOldBits(i) <> lpNewBits(i) Then Debug.Print Fix(i / 360) '行坐标 Debug.Print i Mod 360 '列坐标 End If Next End SubPrivate Sub Form_Load() Set Me.OldPicture.Picture = LoadPicture("原来的圆形图片文件") Set Me.NewPicture.Picture = LoadPicture("修改后的圆形图片文件") End Sub
坐标修正: 行坐标是从0开始算的,如果要从1开始,可以将Debug.Print Fix(i / 360)改为 Fix(i/360)+1, 列坐标为0时,其实是360,因此改为Debug.print iif( (i mod 360)=0,360,i mod 360)
Option Explicit Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As LongPrivate Const ARRAY_SIZE As Long = 129600 '360*360*4Private Sub Command1_Click() Dim i As Long Dim lpOldBits(1 To ARRAY_SIZE) As Long Dim lpNewBits(1 To ARRAY_SIZE) As Long
For i = 1 To ARRAY_SIZE Step 4 If lpOldBits(i) <> lpNewBits(i) Then Debug.Print "坐标位置:" & CStr(Fix(i / 360) + 1) & "行," & CStr(IIf((i Mod 360) = 0, 360, i Mod 360)) & "列" End If DoEvents Next End SubPrivate Sub Form_Load() Me.OldPicture.AutoRedraw = True Me.NewPicture.AutoRedraw = True Set Me.OldPicture.Picture = LoadPicture("d:\旧圆.bmp") Set Me.NewPicture.Picture = LoadPicture("d:\新圆.bmp") End Sub
忘了坐标除4了,最后修改如下: Debug.Print "坐标位置:" & CStr(Fix(i / 360 / 4) + 1) & "行," & CStr(IIf((i / 4 Mod 360) = 0, 360, i / 4 Mod 360)) & "列"
必须使用位图,否则会由于失真造成误差。 Option Explicit Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long Private Const ARRAY_SIZE As Long = 129600Private Sub Command1_Click() Dim i As Long Dim lpOldBits(1 To ARRAY_SIZE) As Long Dim lpNewBits(1 To ARRAY_SIZE) As Long Dim hCOOR As Long, vCOOR As Long
GetBitmapBits Me.OldPicture.Picture.Handle, ARRAY_SIZE * 4, lpOldBits(1) GetBitmapBits Me.NewPicture.Picture.Handle, ARRAY_SIZE * 4, lpNewBits(1) For i = 1 To ARRAY_SIZE If lpOldBits(i) <> lpNewBits(i) Then hCOOR = Fix(i / 360) + 1 vCOOR = i Mod 360 Debug.Print "坐标位置为" & hCOOR & "行" & vCOOR & "列" End If Next End SubPrivate Sub Form_Load() Me.OldPicture.AutoRedraw = True Me.NewPicture.AutoRedraw = True Set Me.OldPicture.Picture = LoadPicture("d:\旧圆.bmp") Set Me.NewPicture.Picture = LoadPicture("d:\新圆.bmp") End Sub
首先,使用GetBitmapBits将原来的空白圆形存储到一个Byte数组中,圆形最好为正圆形。
然后,使用同样的API函数将修改后的圆形(包括用户绘的点线)存储到另一个Byte数组中。
最后,进行比较。以每4个Byte一组循环比较两个数组(4个Byte表示一个象素,可以使用MemoryCopy转换成Long),如果两组相同,说明是原来的图形,不予记录,如果不同,则根据圆形的宽度计算行坐标,圆形的高度计算列坐标,假设空白圆形的高和宽均为10,这时图形数组应该是10*10*4个Byte,行坐标即为数组索引index*10,列坐标为数组索引index mod 10。
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As LongPrivate Const ARRAY_SIZE As Long = 129600 '360*360*4Private Sub Command1_Click()
Dim i As Long
Dim lpOldBits(1 To ARRAY_SIZE) As Long
Dim lpNewBits(1 To ARRAY_SIZE) As Long
Me.ScaleMode = vbPixels
Me.OldPicture.AutoRedraw = True
Me.OldPicture.Width = 360
Me.OldPicture.Height = 360
Me.NewPicture.AutoRedraw = True
Me.OldPicture.Width = 360
Me.OldPicture.Height = 360
GetBitmapBits Me.OldPicture.Picture.Handle, ARRAY_SIZE, lpOldBits(1)
GetBitmapBits Me.NewPicture.Picture.Handle, ARRAY_SIZE, lpNewBits(1)
For i = 1 To ARRAY_SIZE Step 4
If lpOldBits(i) <> lpNewBits(i) Then
Debug.Print Fix(i / 360) '行坐标
Debug.Print i Mod 360 '列坐标
End If
Next
End SubPrivate Sub Form_Load()
Set Me.OldPicture.Picture = LoadPicture("原来的圆形图片文件")
Set Me.NewPicture.Picture = LoadPicture("修改后的圆形图片文件")
End Sub
行坐标是从0开始算的,如果要从1开始,可以将Debug.Print Fix(i / 360)改为 Fix(i/360)+1,
列坐标为0时,其实是360,因此改为Debug.print iif( (i mod 360)=0,360,i mod 360)
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As LongPrivate Const ARRAY_SIZE As Long = 129600 '360*360*4Private Sub Command1_Click()
Dim i As Long
Dim lpOldBits(1 To ARRAY_SIZE) As Long
Dim lpNewBits(1 To ARRAY_SIZE) As Long
Me.ScaleMode = vbPixels
Me.OldPicture.Width = 360
Me.OldPicture.Height = 360
Me.OldPicture.Width = 360
Me.OldPicture.Height = 360
GetBitmapBits Me.OldPicture.Picture.Handle, ARRAY_SIZE, lpOldBits(1)
GetBitmapBits Me.NewPicture.Picture.Handle, ARRAY_SIZE, lpNewBits(1)
For i = 1 To ARRAY_SIZE Step 4
If lpOldBits(i) <> lpNewBits(i) Then
Debug.Print "坐标位置:" & CStr(Fix(i / 360) + 1) & "行," & CStr(IIf((i Mod 360) = 0, 360, i Mod 360)) & "列"
End If
DoEvents
Next
End SubPrivate Sub Form_Load()
Me.OldPicture.AutoRedraw = True
Me.NewPicture.AutoRedraw = True
Set Me.OldPicture.Picture = LoadPicture("d:\旧圆.bmp")
Set Me.NewPicture.Picture = LoadPicture("d:\新圆.bmp")
End Sub
Debug.Print "坐标位置:" & CStr(Fix(i / 360 / 4) + 1) & "行," & CStr(IIf((i / 4 Mod 360) = 0, 360, i / 4 Mod 360)) & "列"
Option Explicit
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Const ARRAY_SIZE As Long = 129600Private Sub Command1_Click()
Dim i As Long
Dim lpOldBits(1 To ARRAY_SIZE) As Long
Dim lpNewBits(1 To ARRAY_SIZE) As Long
Dim hCOOR As Long, vCOOR As Long
GetBitmapBits Me.OldPicture.Picture.Handle, ARRAY_SIZE * 4, lpOldBits(1)
GetBitmapBits Me.NewPicture.Picture.Handle, ARRAY_SIZE * 4, lpNewBits(1)
For i = 1 To ARRAY_SIZE
If lpOldBits(i) <> lpNewBits(i) Then
hCOOR = Fix(i / 360) + 1
vCOOR = i Mod 360
Debug.Print "坐标位置为" & hCOOR & "行" & vCOOR & "列"
End If
Next
End SubPrivate Sub Form_Load()
Me.OldPicture.AutoRedraw = True
Me.NewPicture.AutoRedraw = True
Set Me.OldPicture.Picture = LoadPicture("d:\旧圆.bmp")
Set Me.NewPicture.Picture = LoadPicture("d:\新圆.bmp")
End Sub