-
Aug 2nd, 2014, 02:50 PM
#1
Thread Starter
Addicted Member
A Quick Saving Question *Solved*
Hello! What I am trying to do is to have 1 button in a form save both a bitmap and a text file in the same folder with just one click. So what I wanted it to do was to open a FolderBrowserDialog for the user to select a folder to save them in. I get the MsgBox saying "done" at the end though, but I see no created files.
Nothing is saved with this code:
vb Code:
Private Sub ButtonSave_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSave.Click
If MessageBox.Show("Would you like to save?", "Save As...", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
MessageBox.Show("Please choose a folder.", "Save As...", MessageBoxButtons.OK)
If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim Map As New Bitmap(PanelFullMapToSave.Width, PanelFullMapToSave.Height)
PanelFullMapToSave.DrawToBitmap(Map, New Rectangle(0, 0, PanelFullMapToSave.Width, PanelFullMapToSave.Height))
Map.Save(FolderBrowserDialog1.SelectedPath & "bitmap.png", System.Drawing.Imaging.ImageFormat.Png)
Dim SW As System.IO.StreamWriter = New System.IO.StreamWriter(FolderBrowserDialog1.SelectedPath & "data.txt")
SW.WriteLine(Data)
SW.Dispose()
MsgBox("done")
End If
End If
End Sub
HOWEVER I had previously used this code using a SaveFileDialog, and it worked (saved the bitmap in the location.) In this case I had only saved the bitmap.
vb Code:
Private Sub ButtonSave_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSave.Click
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim Map As New Bitmap(PanelFullMapToSave.Width, PanelFullMapToSave.Height)
PanelFullMapToSave.DrawToBitmap(Map, New Rectangle(0, 0, PanelFullMapToSave.Width, PanelFullMapToSave.Height))
Map.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png)
End If
End Sub
Did I miss something in the first block of code? Thanks.
Last edited by NinjaNic; Aug 2nd, 2014 at 03:37 PM.
-
Aug 2nd, 2014, 03:01 PM
#2
Re: A Quick Saving Question
Debug the file path. It's wrong. You should always debug.
Code:
Dim path As String = IO.Path.Combine(fbd.SelectedPath, "image.jpeg")
-
Aug 2nd, 2014, 03:37 PM
#3
Thread Starter
Addicted Member
Re: A Quick Saving Question
Solved! I forgot the back slash. Thanks for the help!
-
Aug 2nd, 2014, 03:39 PM
#4
Re: A Quick Saving Question *Solved*
-
Aug 2nd, 2014, 03:48 PM
#5
Re: A Quick Saving Question
Originally Posted by NinjaNic
Solved! I forgot the back slash. Thanks for the help!
NO!
ident showed you the correct way, use IO.Path.Combine, otherwise if you add a backslash to your code and the user happens to select the root drive as the destination then your code will most likely fail since the file path would then contain two backslashes in it.
EDIT: Although it may not always matter, under Win7 using IO.File this still works without error!
IO.File.WriteAllText("M:\\\New folder\\OK.TXT", "Testing 1,2,3.")
Last edited by Edgemeal; Aug 2nd, 2014 at 04:00 PM.
-
Aug 2nd, 2014, 05:05 PM
#6
Thread Starter
Addicted Member
Re: A Quick Saving Question *Solved*
Thanks, I read his code wrong and put the & instead of a comma. Now I can remove the back slash and it works.
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
|