我觉得很简单的,就是有一个图片,里面写的是一排汉字,我想取字的边界如上下第一个不是白色的像素位置及为边界.本想从上读取上边界,从下读取下边界,可是从下到上的读取值颜色值总是-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

解决方案 »

  1.   

    把PICTURE控件的boardstyle设为0,scalemode设为3-plxel
    读的时候:
    for x=0 to picture1.scalewidth-1
       for y=0 to picture1.scaleheight-1
          ...
       next
    next
    这是从左上角向右下角,也可以倒过来读,不过要将循环步长设为-1
      

  2.   

    还有,不知道你这个twovalue是什么东东。
    建议在
    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分量保存在数组的过程,直接调用就可以
      

  3.   

    如果图片小的话可用Getpiexl,大的话用模拟指针
      

  4.   

    一般来说,是hdc的问题,另外,你的i和j反了,在getpixel里面第一个是x第二个是y
      

  5.   

    twovalue就是把取颜色的函数放入的模块名
      

  6.   

    谢谢 thirdapple,的确是写反了,问题解决了,真的好高兴呀
    另外我还想问问为什么要把borderstyle设为无边界?
      

  7.   

    you may be in form_load write the function
    or your pic is not visible