Saving a picture without manually choosing the location?
I'm making a screen capturing program, and I am trying to save the image. I can do it as a 'save as' like this
save as Code:
savefiledialog1.Title = "Save File"
savefiledialog1.FileName = "Capture"
savefiledialog1.Filter = "JPEG (*.jpg;*.jpeg;*.jpe;*.jfif)|*.jpg;*.jpeg;*.jpe;*.jfif|PNG (*.png)|*.png"
If savefiledialog1.ShowDialog() = DialogResult.OK Then
PictureBox1.Image.Save(savefiledialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp)
End If
How could I do a quick save option so that it auto saves to a certain location that the user doesn't have to choose and as a .jpg?
Sorry if it's really simple, I've only recently started learning it at college
Re: Saving a picture without manually choosing the location?
You are using a save file dialog so obviously it will ask you where to save the file. That's the point of it.
Code:
PictureBox1.Image.Save(savefiledialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp)
savefiledialog1.FileName is the path and file name. Hard code that.
Side note look at your last entry. "System.Drawing.Imaging.ImageFormat.Bmp"
Re: Saving a picture without manually choosing the location?
I can do the file name, I am unsure how to add the location in?
I have
Quick save Code:
quicksave.FileName = "screenshot" & intCount + 1
PictureBox1.Image.Save(quicksave.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
and I want it to save to "C:\Users\Karl\Desktop\Screenshot"?
Re: Saving a picture without manually choosing the location?
What is this quicksave.filename and intCount +1 all about? As i previously said you need to hard code the location. When i say hard code write the string value exactly as how you would see it.
For example if you wanted to save the picture from multiple instances in your application with possibly a different location each time you can make a new method as such.
vb Code:
Private Sub SavePictureBox(ByVal path As String)
PictureBox1.Image.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg)
End Sub
Now when calling this method you would pass an argument.
vb Code:
SavePictureBox("C:\Users\Karl\Desktop\Screenshot\filename.jpeg")
vb Code:
PictureBox1.Image.Save("C:\Users\Karl\Desktop\Screenshot\filename.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
Re: Saving a picture without manually choosing the location?
Ahh thank you, I misunderstood the first time, it is working now.
The intCount is the number of files in that folder, so by adding 1 to it, it should be a unique filename.
I did it like this to include the intCount
vb Code:
PictureBox1.Image.Save("C:\Users\Karl\Desktop\Screenshot\" & quicksave.FileName & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
Re: Saving a picture without manually choosing the location?
If you want an unique count why not time stamp it? But still what is this quicksave.filename? Can you please post your full code.
Code:
PictureBox1.Image.Save(String.Format("C:\Users\Karl\Desktop\Screenshot\{0}{1}.jpg", quicksave.FileName, intCount + 1), System.Drawing.Imaging.ImageFormat.Jpeg)
Re: Saving a picture without manually choosing the location?
I can't remember why it was originally called "quicksave.filename" it is now strFilename
all code Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapture.Click
Me.Opacity = 0
Timer1.Enabled = True
btnSave.Enabled = True
btnQuickSave.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim bounds As Rectangle
Dim screenshot As System.Drawing.Bitmap
Dim graph As Graphics
bounds = Screen.PrimaryScreen.Bounds
screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
PictureBox1.Image = screenshot
Timer1.Enabled = False
Me.Opacity = 100
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
'If the directory doesn't exist, create it
If Not My.Computer.FileSystem.DirectoryExists("C:\Users\Karl\Desktop\Screenshot") Then
My.Computer.FileSystem.CreateDirectory("C:\Users\Karl\Desktop\Screenshot")
End If
'Count how many files are in the directory
Dim counter = My.Computer.FileSystem.GetFiles("C:\Users\Karl\Desktop\Screenshot")
Dim intCount As Integer
intCount = CStr(counter.Count)
'Save dialog
Dim savefiledialog1 As New SaveFileDialog
savefiledialog1.Title = "Save File"
savefiledialog1.FileName = "Capture" & intCount + 1
savefiledialog1.Filter = "JPEG (*.jpg;*.jpeg;*.jpe;*.jfif)|*.jpg;*.jpeg;*.jpe;*.jfif|PNG (*.png)|*.png"
If savefiledialog1.ShowDialog() = DialogResult.OK Then
PictureBox1.Image.Save(savefiledialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp)
End If
End Sub
Private Sub btnQuickSave_Click(sender As System.Object, e As System.EventArgs) Handles btnQuickSave.Click
Dim strfilename As String
If Not My.Computer.FileSystem.DirectoryExists("C:\Users\Karl\Desktop\Screenshot") Then
My.Computer.FileSystem.CreateDirectory("C:\Users\Karl\Desktop\Screenshot")
End If
'Count how many files are in the directory
Dim counter = My.Computer.FileSystem.GetFiles("C:\Users\Karl\Desktop\Screenshot")
Dim intCount As Integer
intCount = CStr(counter.Count)
strFilename = "screenshot" & intCount + 1
PictureBox1.Image.Save("C:\Users\Karl\Desktop\Screenshot\" & strFilename & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
End Sub
End Class
1 Attachment(s)
Re: Saving a picture without manually choosing the location?
btw - there is a set of methods available that will combine paths and filenames - part of the System.IO namespace
they work like this:
Code:
Dim strReportFolder As String = System.Web.Configuration.WebConfigurationManager.AppSettings("reportfolder")
Dim strStagingFolder As String = Path.Combine(strReportFolder, "staging")
Dim strBusyFile As String = Path.Combine(strStagingFolder, "Busy.txt")
That first Dim gets the path from a web.config file - you could just as easily hardwire that but if you are going to run this app elsewhere you need a way to make that more "variable"
Code:
<appSettings>
<add key="reportfolder" value="D:\ACS Desktop\AWC\reporting"/>
Re: Saving a picture without manually choosing the location?
Quote:
Originally Posted by
szlamany
btw - there is a set of methods available that will combine paths and filenames - part of the System.IO namespace
they work like this:
Code:
Dim strReportFolder As String = System.Web.Configuration.WebConfigurationManager.AppSettings("reportfolder")
Dim strStagingFolder As String = Path.Combine(strReportFolder, "staging")
Dim strBusyFile As String = Path.Combine(strStagingFolder, "Busy.txt")
That first Dim gets the path from a web.config file - you could just as easily hardwire that but if you are going to run this app elsewhere you need a way to make that more "variable"
Code:
<appSettings>
<add key="reportfolder" value="D:\ACS Desktop\AWC\reporting"/>
When you say to use the variable if I want to use it elsewhere, I have now changed it to use environment.username instead of "karl". I am not sure if that is the same thing but programmed differently or if you are talking about different?
Re: Saving a picture without manually choosing the location?
You are hardwiring Desktop\Screenshot though - if it's a personnal use app - or you can force people to create a folder on their desktop (some would say a desktop should only have SHORTCUTS - never a folder) then you have no issues.
Otherwise you should put the path in your app.config files - winforms have one of those - right?