在下面的程序中使用了GetBitmapBits将来自位图的二进制位复制到了PicBits这个数组之中,我想请教一下如何确定在这个数组中任意的值在Picture中的位置,换句话也可以说,我得到了Picture中的X,Y的值,如何得到相应的PicBits的下标。(所有测试均在Picture1.ScaleMode =3环境下)
我发现可以使用下面的的方法得到这个下标的值Picture1.ScaleWidth*Picture1.ScaleHeight*3,认为Picture中的任意一点X,Y的数值就是X*Y*3可是这样根本对应不上啊,希望大家能够帮助一下解决这个问题,谢谢了http://community.csdn.net/Expert/TopicView1.asp?id=4452088这个是另一个提问的帖子,打击也可以在那里回答,如果各位能够帮忙解决这200分都是你的,谢谢'Create a new project, add a command button and a picture box to the project, load a picture into the picture box.
'Paste this code into Form1
Private Type BITMAP
    bmType As Long
    bmWidth As Long
    bmHeight As Long
    bmWidthBytes As Long
    bmPlanes As Integer
    bmBitsPixel As Integer
    bmBits As Long
End Type
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Dim PicBits() As Byte, PicInfo As BITMAP, Cnt As Long
Private Sub Command1_Click()
    'KPD-Team 1999
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    'Get information (such as height and width) about the picturebox
    GetObject Picture1.Image, Len(PicInfo), PicInfo
    'reallocate storage space
    ReDim PicBits(1 To PicInfo.bmWidth * PicInfo.bmHeight * 3) As Byte
    'Copy the bitmapbits to the array
    GetBitmapBits Picture1.Image, Ubound(PicBits), PicBits(1)
    'Invert the bits
    For Cnt = 1 To Ubound(PicBits)
        PicBits(Cnt) = 255 - PicBits(Cnt)
    Next Cnt
    'Set the bits back to the picture
    SetBitmapBits Picture1.Image, Ubound(PicBits), PicBits(1)
    'refresh
    Picture1.Refresh
End Sub

解决方案 »

  1.   


    Dim PicBits() As Byte, PicInfo As BITMAP
    Dim Cnt As Long, BytesPerLine as Long
    Private Sub Command1_Click()
        GetObject Picture1.Image, Len(PicInfo), PicInfo
        BytesPerLine = (PicInfo.bmWidth * 3 + 3) And &HFFFFFFFC
        ReDim PicBits(1 To BytesPerLine * PicInfo.bmHeight * 3) As Byte
        GetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1)
            MsgBox GetPixel(Picture1.hdc, 0, 0) Mod 256   红色
      MsgBox PicBits(3)
      
      MsgBox (GetPixel(Picture1.hdc, 0, 0) And &HFF00&) / 2 ^ 8   绿色
      MsgBox PicBits(2)
        
      MsgBox (GetPixel(Picture1.hdc, 0, 0) And &HFF0000) / 2 ^ 16  蓝色
      MsgBox PicBits(1)
      For Cnt = 1 To UBound(PicBits)
            PicBits(Cnt) = 255 - PicBits(Cnt)
        Next Cnt
          SetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1)
        Picture1.Refresh
    End Sub参考zyl910写的那篇高效的vb图像处理文章吧。
      

  2.   

    还是推荐我自己,呵呵: http://blog.csdn.net/wallescai/
    主要看看效果处理的几篇,都是和像素的位置有关的
      

  3.   

    to:laviewpbt(人一定要靠自己) 您好,我从前测试的时候是完整的啊,可是我刚才又测试了一下,却不行了,我从前眼花??,看来可能是我太错心了
      MsgBox GetPixel(Picture1.hdc, 0, 0) Mod 256   红色
      MsgBox PicBits(3)
      
      MsgBox (GetPixel(Picture1.hdc, 0, 0) And &HFF00&) / 2 ^ 8   绿色
      MsgBox PicBits(2)
        
      MsgBox (GetPixel(Picture1.hdc, 0, 0) And &HFF0000) / 2 ^ 16  蓝色
      MsgBox PicBits(1)
    我改成:
      MsgBox GetPixel(Picture1.hdc, 1, 0) Mod 256   '红色
      MsgBox PicBits(6)
      
      MsgBox (GetPixel(Picture1.hdc, 1, 0) And &HFF00&) / 2 ^ 8   '绿色
      MsgBox PicBits(5)
        
      MsgBox (GetPixel(Picture1.hdc, 1, 0) And &HFF0000) / 2 ^ 16  '蓝色
      MsgBox PicBits(4)
    为什么就不可以了,数值就不是一一对应了,麻烦您解释一下可以么,我换了图片他的
      MsgBox GetPixel(Picture1.hdc, 0, 0) Mod 256   红色
      MsgBox PicBits(3)
      
      MsgBox (GetPixel(Picture1.hdc, 0, 0) And &HFF00&) / 2 ^ 8   绿色
      MsgBox PicBits(2)
        
      MsgBox (GetPixel(Picture1.hdc, 0, 0) And &HFF0000) / 2 ^ 16  蓝色
      MsgBox PicBits(1)
    出来的数值也不同,这个到底是为什么呢,我找到的资料说象素起始点应该是一样的,是不是错误的啊,还有这个象素是如何排列的呢是不是从左到右没,从上到下啊,谢谢您回答一下TO :lesCai(曾经沧海难为水,除却巫山不是云。此情可待成追忆,只是):
    谢谢您的两次回帖,我上次就去了,不过我依然没找到象素的存储规律,您的所有的程序代码我一个都没调试成功,呵呵,您能不能帮忙说一下象素的存储方式呢?谢谢
      

  4.   

    你去看一下bmp的结构就清楚了。
      
    这样是对应的    
      MsgBox GetPixel(Picture1.hDC, 0, 1) Mod 256
      MsgBox PicBits(7)
      
      MsgBox (GetPixel(Picture1.hDC, 0, 1) And &HFF00&) / 2 ^ 8
      MsgBox PicBits(6)
        
      MsgBox (GetPixel(Picture1.hDC, 0, 1) And &HFF0000) / 2 ^ 16
      MsgBox PicBits(5)我觉得用DIB可能方便点。可以参考lesCai的方法。我用的读取方法也和他差不多。需要可以联系
    33184777。
      

  5.   

    抱歉这几天总是上线回不了帖
    我真的只是想知道象素和X,Y值的对应关系,谢谢大家了,lesCai和zyl910的文章还有bmp的结构我都看了很多遍,我始终没有看到这个问题,大家能不能来点干粮啊,光是代码和一些连接我实在是……了谢谢大家了
      

  6.   

    尤其是wallscai能否给出源代码啊?
    真的很感谢啊!!