Results 1 to 10 of 10

Thread: how load jpg into picbox @ runtime

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    shiraz
    Posts
    163

    Question how load jpg into picbox @ runtime

    hi
    i want to load jpg and gif files into a picturebox at run time. but i don't know how or i have forgot. can u please help
    thanks

  2. #2
    Hyperactive Member Eyes.Only's Avatar
    Join Date
    Oct 2001
    Location
    Minnesota
    Posts
    347
    VB Code:
    1. Picture1.Picture = LoadPicture("myPic.jpg")   'open pic.
    2.  
    3. Picture1.Picture = LoadPicture()   'Clear the picturebox.

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Or you can load it into a DC with this and then BitBlt the image into the picturebox

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDc As Long) As Long
    4. Private Declare Function DeleteDC Lib "gdi32" (ByVal hDc As Long) As Long
    5. Private 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
    6. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    7. Private Declare Function SelectObject Lib "gdi32" (ByVal hDc As Long, ByVal hObject As Long) As Long
    8. Private 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
    9.  
    10. Public Function GenerateDC(FileName As String) As Long
    11.     Dim DC As Long, picTemp As IPictureDisp
    12.     DC = CreateCompatibleDC(0)
    13.     If DC < 1 Then
    14.         Exit Function
    15.     End If
    16.     Set picTemp = LoadPicture(FileName)
    17.     SelectObject DC, picTemp
    18.     DeleteObject picTemp
    19.     Set picTemp = Nothing
    20.     GenerateDC = DC
    21. End Function
    22.  
    23. Private Sub Form_Load()
    24.     Dim myDc As Long
    25.     myDc = GenerateDC("c:\jamie\jamie.bmp")
    26.     With Picture1
    27.         .AutoRedraw = True
    28.         BitBlt .hDc, 0, 0, .Width, .Height, myDc, 0, 0, vbSrcCopy
    29.     End With
    30. End Sub

  4. #4
    Hyperactive Member Eyes.Only's Avatar
    Join Date
    Oct 2001
    Location
    Minnesota
    Posts
    347
    ...showoff...

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by Eyes.Only
    ...showoff...
    oh yeah

  6. #6
    Hyperactive Member Eyes.Only's Avatar
    Join Date
    Oct 2001
    Location
    Minnesota
    Posts
    347
    quick question tho...what does that do for you? your still using LoadPicture inside your generateDC function so i fail to see the benefit..

    explain to me smartypants! explain!

  7. #7
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Because you've now loaded the image into an offscreen device context, which means that you can now paint it (very fast) by using BitBlt into any pictureboxes you want.
    Thats how you do 2d graphics in games. You load the images into a DC, then BitBlt them later onto the screen.

    If you goto my website and scroll to the very right, you'll see how I load all the images for the ships and what not into DCs for use later.

  8. #8
    Hyperactive Member Eyes.Only's Avatar
    Join Date
    Oct 2001
    Location
    Minnesota
    Posts
    347
    oh yea? well ...well...my dad could beat up your dad!

    haha

    actually thats a cool thing to know, i havent done much with DCs in vb...i almost forgot about them. heh

  9. #9
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Yup handy little buggers. If you look in the Picutre Resizing/Changing thread (or something along those lines) you'll see lots more code for device contexts, and also code for using the StretchBlt and SetStretchBltMode functions

  10. #10
    Hyperactive Member Eyes.Only's Avatar
    Join Date
    Oct 2001
    Location
    Minnesota
    Posts
    347
    i always wanted to load a pic into dc without LoadPicture because its so slow when dealing with a lot of pictures.

    i also remember wanting to create gif files without the lzw compression, but i ended up having to create the images through a dll i made in vc++ and then use api to access it through my vb...

    they just need better gfx handling functions in vb.

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