Jun 14th, 2006, 05:14 AM
#1
Thread Starter
Junior Member
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
Jun 14th, 2006, 06:04 AM
#2
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:
Picture1.Picture = Picture1.Image
' Then save it
The files will be saved as BMP files, no matter what extension you put on them.
Jun 14th, 2006, 06:20 AM
#3
Thread Starter
Junior Member
Thanks!
Thanks a bunch! I don't have VB6 at home, only at school, so ill try it out tomorrow. Thanks!
Jun 15th, 2006, 08:22 AM
#4
Thread Starter
Junior Member
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!
Aug 22nd, 2006, 04:15 AM
#5
Thread Starter
Junior Member
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.
Aug 22nd, 2006, 06:11 AM
#6
Re: Save/load images in a Picturebox using standard code
Is this what you mean?
VB Code:
Private Sub Command1_Click()
Picture1.Picture = LoadPicture("c:\123.bmp")
End Sub
Private Sub Command2_Click()
SavePicture Picture1.Picture, "c:\321.bmp"
End Sub
Aug 24th, 2006, 03:54 AM
#7
Thread Starter
Junior Member
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]
Aug 24th, 2006, 04:26 AM
#8
PowerPoster
Re: Save/load images in a Picturebox using standard code
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.
Aug 24th, 2006, 04:33 AM
#9
Re: Save/load images in a Picturebox using standard code
VB Code:
' assuming Dialog is the name of the Common Dialog that has been added to the form
Private Sub Command1_Click()
' I hope the syntax is right, it has been a while I used the control
With Dialog
.CancelError = True
.Filter = "Windows bitmap files (*.bmp)|*.bmp|All files (*.*)|*.*"
.Title = "This is my open dialog!"
On Error Goto NoFileOpened
.ShowOpen
MsgBox "You selected file " & .Filename
NoFileOpened:
End With
End Sub
Aug 24th, 2006, 05:05 AM
#10
PowerPoster
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:
' assuming Dialog is the name of the Common Dialog that has been added to the form
Private Sub Command1_Click()
Dim strFileName As String
' I hope the syntax is right, it has been a while I used the control
With Dialog
.CancelError = True
.Filter = "Windows bitmap files (*.bmp)|*.bmp|All files (*.*)|*.*"
.[hl]Dialog[/hl]Title = "This is my open dialog!"
.InitDir = "C:\" 'this will open the dialog to a default directory location
On Error Goto NoFileOpened
.ShowOpen
strFileName = .FileName
NoFileOpened:
End With
'load the picture to the picturebox
pbPicure.Picture = LoadPicture(strFileName)
End Sub
Originally Posted by
Merri
VB Code:
' assuming Dialog is the name of the Common Dialog that has been added to the form
Private Sub Command1_Click()
' I hope the syntax is right, it has been a while I used the control
With Dialog
.CancelError = True
.Filter = "Windows bitmap files (*.bmp)|*.bmp|All files (*.*)|*.*"
.Title = "This is my open dialog!"
On Error Goto NoFileOpened
.ShowOpen
MsgBox "You selected file " & .Filename
NoFileOpened:
End With
End Sub
Last edited by BrailleSchool; Aug 24th, 2006 at 05:15 AM .
Aug 24th, 2006, 05:15 AM
#11
PowerPoster
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
Aug 25th, 2006, 03:58 AM
#12
Thread Starter
Junior Member
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]
Aug 25th, 2006, 04:08 AM
#13
Re: Save/load images in a Picturebox using standard code
VB Code:
Public lx As Long
Public ly As Long
Private Sub Command1_Click()
'You can defing A fixed sizew logo using this code
On Error GoTo MyError
CommonDialog1.Filter = CommonDialog1.Filter = "Image Files Only|*.bmp;*.gif;*.jpg;*.jpeg"
CommonDialog1.Flags = cdlOFNFileMustExist Or cdlOFNPathMustExist
CommonDialog1.ShowOpen
If CommonDialog1.FileName <> "" Then
FileName1 = CommonDialog1.FileName
If FileLen(FileName1) > 30000 Then
FileName1 = ""
MsgBox "Please Check Image Size Again...", vbInformation, "Incorrect Size Of Image"
Image1 = LoadPicture("")
FileName1 = ""
Else
Image1 = LoadPicture(FileName1)
End If
Else
FileName1 = ""
End If
'Sedding Image or Logo To The Center Of Image Box use this code
'Code By shakti singh dulawat
'With Img
'lx = 0: ly = 0
'If .Width < Picture1.Width Then lx = (Picture1.Width - .Width) / 2
'If .Height < Picture1.Height Then ly = (Picture1.Height - .Height) / 2
'.Move lx, ly, .Width, .Height
'End With
MyError:
If Err.Number <> 0 Then
If Err.Number = 32755 Then Exit Sub
MsgBox Err.Number & " - " & Err.Description, vbCritical, "Please Select Correct Image"
End If
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
Forum Rules
Click Here to Expand Forum to Full Width