我觉得很简单的,就是有一个图片,里面写的是一排汉字,我想取字的边界如上下第一个不是白色的像素位置及为边界.本想从上读取上边界,从下读取下边界,可是从下到上的读取值颜色值总是-1,对于比较长的图像总也读不出来,读颜色用的getpixel函数,还有颜色是-1,叫屏蔽色,是干什么的?真不知道错在哪里了?总是取出-1的值。
调用: HeightNum Picture1.HDC, Picture1.Width \ Screen.TwipsPerPixelX, Picture1.Height \ Screen.TwipsPerPixelYPublic Function HeightNum(ByVal HDC&, ByVal nWidth&, ByVal nHeight&) As Long
Dim i As Long
Dim j As Long
Dim rgbColor As Long
Dim RValue As Long
Dim GValue As Long
Dim BValue As Long
Dim flag As Boolean
Dim top As Long
Dim bottom As Long
flag = False
' 求上边界
rgbColor = 0
For i = 1 To nHeight - 2
For j = 1 To nWidth - 2
rgbColor = GetPixel(HDC, i, j)
If rgbColor = -1 Then GoTo continue RValue = twovalue.GetRValue(rgbColor)
GValue = twovalue.GetGValue(rgbColor)
BValue = twovalue.GetBValue(rgbColor) If (RValue + GValue + BValue) / 3 <> 255 Then
flag = True
Exit For
End If
continue:
Next j
If flag = True Then
Exit For
End If
Next i
'保存i的值
top = i
'求下边界
flag = False
For i = nHeight - 2 To top Step -1
For j = 1 To nWidth - 2
rgbColor = GetPixel(HDC, i, j)
If rgbColor = -1 Then GoTo conti
RValue = twovalue.GetRValue(rgbColor)
GValue = twovalue.GetGValue(rgbColor)
BValue = twovalue.GetBValue(rgbColor)
If (RValue + GValue + BValue) / 3 <> 255 Then
flag = True
Exit For
End If
conti:
Next j
If flag = True Then
Exit For
End If
Next i
bottom = i
HeightNum = bottom-top
调用: HeightNum Picture1.HDC, Picture1.Width \ Screen.TwipsPerPixelX, Picture1.Height \ Screen.TwipsPerPixelYPublic Function HeightNum(ByVal HDC&, ByVal nWidth&, ByVal nHeight&) As Long
Dim i As Long
Dim j As Long
Dim rgbColor As Long
Dim RValue As Long
Dim GValue As Long
Dim BValue As Long
Dim flag As Boolean
Dim top As Long
Dim bottom As Long
flag = False
' 求上边界
rgbColor = 0
For i = 1 To nHeight - 2
For j = 1 To nWidth - 2
rgbColor = GetPixel(HDC, i, j)
If rgbColor = -1 Then GoTo continue RValue = twovalue.GetRValue(rgbColor)
GValue = twovalue.GetGValue(rgbColor)
BValue = twovalue.GetBValue(rgbColor) If (RValue + GValue + BValue) / 3 <> 255 Then
flag = True
Exit For
End If
continue:
Next j
If flag = True Then
Exit For
End If
Next i
'保存i的值
top = i
'求下边界
flag = False
For i = nHeight - 2 To top Step -1
For j = 1 To nWidth - 2
rgbColor = GetPixel(HDC, i, j)
If rgbColor = -1 Then GoTo conti
RValue = twovalue.GetRValue(rgbColor)
GValue = twovalue.GetGValue(rgbColor)
BValue = twovalue.GetBValue(rgbColor)
If (RValue + GValue + BValue) / 3 <> 255 Then
flag = True
Exit For
End If
conti:
Next j
If flag = True Then
Exit For
End If
Next i
bottom = i
HeightNum = bottom-top
读的时候:
for x=0 to picture1.scalewidth-1
for y=0 to picture1.scaleheight-1
...
next
next
这是从左上角向右下角,也可以倒过来读,不过要将循环步长设为-1
建议在
RValue = twovalue.GetRValue(rgbColor)
GValue = twovalue.GetGValue(rgbColor)
BValue = twovalue.GetBValue(rgbColor)
处加上断点,运行到这里的时候查看一下rgbColor的值。
另外用GETPIXEL方法获得像素值是很慢很慢的方法。
给你一个比较快速的方法:http://community.csdn.net/Expert/topic/3252/3252001.xml?temp=.5707971
里面包含了一个从PICTRUE控件获得图片,并以R,G,B分量保存在数组的过程,直接调用就可以
另外我还想问问为什么要把borderstyle设为无边界?
or your pic is not visible