Results 1 to 2 of 2

Thread: About GetBitmapBits and SetBitmapBits

  1. #1

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Unhappy About GetBitmapBits and SetBitmapBits

    Hello there again, guys...

    I am still trying the zoom thing, but I cannot get this thing to work. I use GetBitMapBits and it gets an array of Bytes corresponding to the picture given. Now I tried to get a part of that array and display it. So far so good...

    When I give the default values (It is the Picture's Width and the Picture's Height) it shows the Picture as it is (Perfect) But if I give other values it simply shows a black background instead of the piece of the picture...

    Please check this code... Thanks in advance:


    VB Code:
    1. '--- Code in BmpModule ---
    2. Private Type BitMap
    3.     bmType As Long
    4.     bmWidth As Long
    5.     bmHeight As Long
    6.     bmWidthBytes As Long
    7.     bmPlanes As Integer
    8.     bmBitsPixel As Integer
    9.     bmBits As Long
    10. End Type
    11.  
    12. Public StartPosX As Single
    13. Public StartPosY As Single
    14. Public EndPosX As Single
    15. Public EndPosY As Single
    16.  
    17. Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
    18. Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
    19. Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
    20. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    21.  
    22. Public Sub GetPart(Picture1 As PictureBox, Picture2 As PictureBox)
    23. Dim bmWidth As Long
    24. Dim bmHeight As Long
    25. Dim bmSize As Long
    26. Dim NewbmSize As Long
    27. Dim bmBits() As Byte
    28.  
    29. Dim bmZoomWidth As Long
    30. Dim bmZoomHeight As Long
    31.  
    32. 'Get picture's Width and Height
    33. bmWidth = Picture1.Width
    34. bmHeight = Picture1.Height
    35.  
    36. bmZoomWidth = EndPosX - StartPosX
    37. bmZoomHeight = EndPosY - StartPosY
    38.  
    39. 'ReDefine Bit array to hold all pixels from picture box
    40. ReDim Bits(0 To bmWidth - 1, 0 To bmHeight - 1) As Byte
    41. 'ReDim BitsZoom(0 To bmZoomWidth - 1, 0 To bmZoomHeight - 1) As Byte
    42. ReDim BitsZoom(StartPosX To EndPosX - 1, StartPosY To EndPosY - 1) As Byte
    43.  
    44. 'Store size of bitmap in total pixels
    45. bmSize = bmWidth * bmHeight
    46.  
    47. 'Grab picture's pixels and load to Bit array
    48. GetBitmapBits Picture1.Image, bmSize, Bits(0, 0)
    49.    
    50. 'Get Bits Selected
    51. Dim Y As Long
    52. Dim X As Long
    53.  
    54. For Y = StartPosY To EndPosY - 1
    55.  For X = StartPosX To EndPosX - 1
    56.   BitsZoom(X - StartPosX, Y - StartPosY) = Bits(X, Y)
    57.  Next X
    58. Next Y
    59.  
    60. 'Load Bit array to picture box
    61. Picture2.Width = bmZoomWidth
    62. Picture2.Height = bmZoomHeight
    63.  
    64. NewbmSize = bmZoomWidth * bmZoomHeight
    65.  
    66. SetBitmapBits Picture2.Image, NewbmSize, BitsZoom(0, 0)
    67.  
    68. 'Redraw
    69. Picture2.Refresh
    70. End Sub
    71.  
    72. '--- Form's Code ---
    73. Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    74. StartPosX = X
    75. StartPosY = Y
    76.  
    77. 'The Shape thing is to provide a sensation of selection
    78. With Shape1
    79.  .Left = X
    80.  .Top = Y
    81.  .Width = 0
    82.  .Height = 0
    83.  .BorderStyle = 5
    84.  .Visible = True
    85.  .Tag = ""
    86. End With
    87. End Sub
    88.  
    89. Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    90. With Shape1
    91.  If Shape1.Visible = True And .Tag = "" Then
    92.   .Width = Abs(StartPosX - X)
    93.   .Height = Abs(StartPosY - Y)
    94.  End If
    95. End With
    96. End Sub
    97.  
    98. Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    99. EndPosX = X
    100. EndPosY = Y
    101. Shape1.Tag = "Selected"
    102. End Sub
    103.  
    104. Private Sub CmdCopyAll_Click()
    105. StartPosY = 0
    106. EndPosY = Picture1.Height
    107. StartPosX = 0
    108. EndPosX = Picture1.Width
    109.  
    110. GetPart Picture1, Picture2
    111. End Sub
    112.  
    113. Private Sub CmdGetPart_Click()
    114. If Shape1.Visible = False Then
    115.  StartPosY = 0
    116.  EndPosY = Picture1.Height
    117.  StartPosX = 0
    118.  EndPosX = Picture1.Width
    119. Else
    120.  Shape1.Visible = False
    121. End If
    122.  
    123. Me.MousePointer = vbHourglass
    124. GetPart Picture1, Picture2
    125. Me.MousePointer = vbDefault
    126. End Sub
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  2. #2

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Arrow PS

    Guys, to check my code please copy the first part to a module and the rest to a form with the following controls:

    1.- 2 PictureBoxes One named "Picture1" and the other "Picture2"
    2.- 1 Shape named "Shape1" which border can be dotted. It is just to show the "Selection"
    3.- 2 Command Buttons named "CmdGetPart" and named "CmdCopyAll"

    The Form's code strarts from where I put the following header:


    VB Code:
    1. '--- Form's Code ---
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width