|
-
Oct 19th, 2012, 09:35 AM
#1
Thread Starter
New Member
How Can I save a picture as a JPEG file? I NEED HELP!!
What I want to do:
I want the user to click on the picture box, an openfiledialog box open, the user choose a photo from a file on their computer, and that picture load in the picture box. Then I want the user the click on a button and a savefiledialog pop up. from there i want the user to be able to type a name and click ok and the picture save as a jpeg, to a file that i specify, to later be opened in another form.
I've got the loading part good. that works fine. I just cant figure out how to save the image that has been
loaded into the picture box. Right now for testing purposes, I have a form with a picturebox, a button, and a savefiledialog on it, with default names.
What I've tried (the numbers after each method corresponds with the errors i got):
the first three were done inside "if savefiledialog1.showdialog() = dialogresult.ok then"
option1
Me.PictureBox1.Image.Save(Application.StartupPath & "\gallery\") (1)
option2
PictureBox1.Image.Save(Application.StartupPath & "\gallery\") (1)
option3
PictureBox1.Image.Save(SaveFileDialog1.FileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg) (1)
option4
with this one i just typed it into the buttons click event.
SaveFileDialog1.InitialDirectory = (Application.StartupPath & "\gallery\")
SaveFileDialog1.FileName = "New Entry"
SaveFileDialog1.Filter = "txt files (*.txt)|*.txt|Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"
SaveFileDialog1.ShowDialog()
Dim w As New IO.StreamWriter(SaveFileDialog1.FileName)
w.Write(PictureBox1.Image)
w.Close() (3)
When i try this one it saves the image as a .bmp, but when i try to open it in paint, i get this windows error: paint cannot read this file. This is not a valid bitmap file or its format is not currently supported.
and when i try to load the saved image into the picturebox (as the user would), i get this error: ArgumentException was unhandled.
parameter is not valid
option5
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"
saveFileDialog1.Title = "Save an Image File"
saveFileDialog1.ShowDialog()
If saveFileDialog1.FileName <> "" Then
Dim fs As System.IO.FileStream = CType _
(saveFileDialog1.OpenFile(), System.IO.FileStream)
Select Case saveFileDialog1.FilterIndex
Case 1
Me.PictureBox1.Image.Save(fs, _
System.Drawing.Imaging.ImageFormat.Jpeg) (1)
Case 2
Me.PictureBox1.Image.Save(fs, _
System.Drawing.Imaging.ImageFormat.Bmp)
Case 3
Me.PictureBox1.Image.Save(fs, _
System.Drawing.Imaging.ImageFormat.Gif)
End Select
fs.Close()
End If
option6
Dim image As image = PictureBox1.Image (2)
image.Save("C:\image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
Errors I've got:
(1) A generic error occured in GDI+
(2) Value of type 'System.Drawing.Image' cannot be converted to 'WindowsApplication1.Image'
(3) ArgumentException was unhandled
parameter is not valid
What am I doing wrong? Is there something I'm missing? Another way I should do this? I've also tried using a button, panel, and a retangle shape instead of a picturebox with no luck. I tried using the backgroundimage property instead of the image prop with no luck. I read somewhere not to use the .load but instead use .fromfile..... again no luck. so I am completely stummped. I've thought about trying to convert the image to bmp since it seems easier to save a bmp, but i really would like the image to be saved as a jpeg. Im using visual basic 2010 express and if anyone could please help me out, I would be most greatful. Thank you and someone please please please help me!!
sorry this was so long. I just wanted to make sure im totally clear on what im trying to accomplish and what i've done so i hopefully get the help i need. Thanks all!!
Tags for this Thread
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
|