Results 1 to 13 of 13

Thread: Save/load images in a Picturebox using standard code

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    17

    Question Save/load images in a Picturebox using standard code

    Hi everyone, first time post here.

    I am making a sort of paint program for a school project and was wondering if there was any way to load or save images (It can be any format, i'm happy with .BMP) from the picture box i'm using for drawing. I hope you can get a quick reply because it's due in 2 days!! Thanks heaps in advance!

    Note: i'm using VB6
    Last edited by iwantanimac; Jun 14th, 2006 at 05:15 AM. Reason: Forgot to state VB version... stupid me

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Save/load images in a Picturebox using standard code



    LoadPicture & SavePicture are what you need - look in your help file

    If you've been drawing on the picturebox then you need to persist the image to the picture:
    VB Code:
    1. Picture1.Picture = Picture1.Image
    2. ' Then save it
    The files will be saved as BMP files, no matter what extension you put on them.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    17

    Talking Thanks!

    Thanks a bunch! I don't have VB6 at home, only at school, so ill try it out tomorrow. Thanks!

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    17

    Re: Save/load images in a Picturebox using standard code

    okay, still not sure how to do it as the help search i did for Savefile only came up with information on a bug in the system... so can anyone be slightly more descriptive. I just want standard load/save dialogs. Please hurry as i have only tomorrow to finish it!

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    17

    Re: Save/load images in a Picturebox using standard code

    I still haven't figured thsi out in VB6... The project is over, though so ill be trying to port the current code to VB 2005 Express, but i keep getting errors. I won't mark this as resolved because i still don't get it.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Save/load images in a Picturebox using standard code

    Is this what you mean?
    VB Code:
    1. Private Sub Command1_Click()
    2. Picture1.Picture = LoadPicture("c:\123.bmp")
    3. End Sub
    4.  
    5. Private Sub Command2_Click()
    6. SavePicture Picture1.Picture, "c:\321.bmp"
    7. End Sub

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    17

    Re: Save/load images in a Picturebox using standard code

    Yeah, is there any way to get a standard Load/Save dialog for it?
    If you do end up answering, please give as much detail as possible because I'm new to VB! Thanks guys!
    My Projects: [TrayPinger]

  8. #8
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Save/load images in a Picturebox using standard code

    Quote Originally Posted by iwantanimac
    Yeah, is there any way to get a standard Load/Save dialog for it?
    use the common dialog control.

    to get this, right click on your toolbox (where the controls are), select components, scroll down till you see "Microsoft Common Dialog Control", put a check in the checkbox to the left of the control, click OK. then you can add the control to your form. you will then need to code to do what you need.

  9. #9
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Save/load images in a Picturebox using standard code

    VB Code:
    1. ' assuming Dialog is the name of the Common Dialog that has been added to the form
    2.  
    3. Private Sub Command1_Click()
    4.     ' I hope the syntax is right, it has been a while I used the control
    5.     With Dialog
    6.         .CancelError = True
    7.         .Filter = "Windows bitmap files (*.bmp)|*.bmp|All files (*.*)|*.*"
    8.         .Title = "This is my open dialog!"
    9.         On Error Goto NoFileOpened
    10.         .ShowOpen
    11.         MsgBox "You selected file " & .Filename
    12. NoFileOpened:
    13.     End With
    14. End Sub

  10. #10
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Save/load images in a Picturebox using standard code

    if you have a picturebox called pbPicture, then you would do something like this:
    VB Code:
    1. ' assuming Dialog is the name of the Common Dialog that has been added to the form
    2.  
    3. Private Sub Command1_Click()
    4.     Dim strFileName As String
    5.     ' I hope the syntax is right, it has been a while I used the control
    6.     With Dialog
    7.         .CancelError = True
    8.         .Filter = "Windows bitmap files (*.bmp)|*.bmp|All files (*.*)|*.*"
    9.         .[hl]Dialog[/hl]Title = "This is my open dialog!"
    10.         .InitDir = "C:\" 'this will open the dialog to a default directory location
    11.         On Error Goto NoFileOpened
    12.         .ShowOpen
    13.         strFileName = .FileName
    14. NoFileOpened:
    15.     End With
    16.    
    17.     'load the picture to the picturebox
    18.     pbPicure.Picture = LoadPicture(strFileName)
    19. End Sub
    Quote Originally Posted by Merri
    VB Code:
    1. ' assuming Dialog is the name of the Common Dialog that has been added to the form
    2.  
    3. Private Sub Command1_Click()
    4.     ' I hope the syntax is right, it has been a while I used the control
    5.     With Dialog
    6.         .CancelError = True
    7.         .Filter = "Windows bitmap files (*.bmp)|*.bmp|All files (*.*)|*.*"
    8.         .Title = "This is my open dialog!"
    9.         On Error Goto NoFileOpened
    10.         .ShowOpen
    11.         MsgBox "You selected file " & .Filename
    12. NoFileOpened:
    13.     End With
    14. End Sub

  11. #11
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Save/load images in a Picturebox using standard code

    by the way Merry, .Title should be .DialogTitle

    here is a working example for you.
    Attached Files Attached Files

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    17

    Re: Save/load images in a Picturebox using standard code

    Awesome guys! Thanks for all the help! =)
    If you do end up answering, please give as much detail as possible because I'm new to VB! Thanks guys!
    My Projects: [TrayPinger]

  13. #13
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: Save/load images in a Picturebox using standard code

    VB Code:
    1. Public lx As Long
    2. Public ly As Long
    3. Private Sub Command1_Click()
    4. 'You can defing A fixed sizew logo using this code
    5. On Error GoTo MyError
    6. CommonDialog1.Filter = CommonDialog1.Filter = "Image Files Only|*.bmp;*.gif;*.jpg;*.jpeg"
    7. CommonDialog1.Flags = cdlOFNFileMustExist Or cdlOFNPathMustExist
    8. CommonDialog1.ShowOpen
    9. If CommonDialog1.FileName <> "" Then
    10. FileName1 = CommonDialog1.FileName
    11.     If FileLen(FileName1) > 30000 Then
    12.     FileName1 = ""
    13.     MsgBox "Please Check Image Size Again...", vbInformation, "Incorrect Size Of Image"
    14.     Image1 = LoadPicture("")
    15.     FileName1 = ""
    16.     Else
    17.     Image1 = LoadPicture(FileName1)
    18.     End If
    19. Else
    20. FileName1 = ""
    21. End If
    22.  
    23. 'Sedding Image or Logo To The Center Of Image Box use this code
    24. 'Code By shakti singh dulawat
    25.  
    26. 'With Img
    27. 'lx = 0: ly = 0
    28. 'If .Width < Picture1.Width Then lx = (Picture1.Width - .Width) / 2
    29. 'If .Height < Picture1.Height Then ly = (Picture1.Height - .Height) / 2
    30. '.Move lx, ly, .Width, .Height
    31. 'End With
    32.  
    33. MyError:
    34. If Err.Number <> 0 Then
    35.    If Err.Number = 32755 Then Exit Sub
    36.    MsgBox Err.Number & " - " & Err.Description, vbCritical, "Please Select Correct Image"
    37. End If
    38. End Sub

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