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
Re: Save/load images in a Picturebox using standard code
http://www.vbforums.com/attachment.p...chmentid=47052
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.
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!
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.
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
Re: Save/load images in a Picturebox using standard code
Yeah, is there any way to get a standard Load/Save dialog for it?
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.
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
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
Quote:
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
1 Attachment(s)
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.
Re: Save/load images in a Picturebox using standard code
Awesome guys! Thanks for all the help! =)
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