Results 1 to 5 of 5

Thread: VB - Load a section of a bitmap file into a PictureBox (TileEngine use)

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229

    VB - Load a section of a bitmap file into a PictureBox (TileEngine use)

    This code will copy a specified rectangle from a bitmap file and place it in the picturebox you specify.

    NOTE: The Picturebox must be a control array with AUTOREDRAW set to TRUE, but you can change the code in order to make them not a control array.

    VB Code:
    1. Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
    2.     Public Const SRCCOPY = &HCC0020
    3.  
    4. Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    5.  
    6. Public Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    7.  
    8. Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    9.  
    10. Public Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
    11.     Public Const IMAGE_BITMAP = 0
    12.     Public Const LR_LOADFROMFILE = &H10
    13.  
    14. Public Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    15.  
    16.  
    17.  
    18. Public Function LoadGraphics(ByVal FileName As String, ByVal PosX As Long, ByVal PosY As Long, ByVal Width As Long, ByVal Height As Long, ControlName As Object)
    19.   On Error GoTo ErrHandler     'Cheap error handling that message boxes the errors.
    20.  
    21.   Dim DC As Long   'Hold the device context of the image
    22.   Dim Image As Long   'Holds the loaded image
    23.   Dim NewTile As Integer   'The control # to use
    24.  
    25.   NewTile = ControlName.Count   'Get the count of present pictureboxes
    26.   Load ControlName(NewTile)   'Load one more Picturebox in the control array
    27.   'ControlName(NewTile).Visible = True   'Make the PB visible (TESTING PURPOSES)
    28.   'ControlName(NewTile).Move ControlName(NewTile - 1).Left + (Width * Screen.TwipsPerPixelX), ControlName(NewTile - 1).Top + (Height * Screen.TwipsPerPixelY), Width * Screen.TwipsPerPixelX, Height * Screen.TwipsPerPixelY  'Move the picturebox (TESTING PURPOSES)
    29.  
    30.   'FileName = App.Path & "\Maps\" & FileName   'Use this line if you only want graphics to be within the app's directory
    31.  
    32.   DC = CreateCompatibleDC(ControlName(NewTile).hdc)   'Create a Device Contect compatible with the PictureBox
    33.  
    34.   Image = LoadImage(0, FileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)   'Load the entire image file
    35.  
    36.   Call SelectObject(DC, Image)   'Assign the image to the device context
    37.  
    38.   Call BitBlt(ControlName(NewTile).hdc, 0, 0, Width, Height, DC, PosX, PosY, SRCCOPY)  'Copy the specified section of the bitmap into the picturebox
    39.  
    40.   'Clean up our tracks
    41.   DeleteDC DC
    42.   DeleteObject Image
    43.  
    44.   Exit Function  'Exit function without launching the error handler
    45.    
    46. ErrHandler:
    47.   MsgBox Err.Number & Err.Description
    48. End Function

    If you don't like my code, I can fix it up for you. All that I know is that I composed it all by myself after hours of searching the net for some help but couldn't find any. BUt thanx to all those who tried to help me.
    Last edited by INF3RN0666; May 14th, 2003 at 06:19 AM.

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