Save an image from a picturebox control every 2 seconds that it updates...
Hi guys :wave i have a picturebox in my form and it receives a new screenshot every 2 seconds from a timer. when the timer interval hits 2000 every time and updates the the picturebox image to the new screenshot image how do i save that image as a jpeg in an order of "ss1.jpeg, ss2.jpeg, ss3.jpeg ect... to a folder specified by the FolderBrowserDialog
Code:
Dim strfilename As String
Dim folder As String
strfilename = "1.jpeg"
folder = FolderBrowserDialog1.SelectedPath
Call Form3.picturebox1.Image.Save(folder & "\" & strfilename, System.Drawing.Imaging.ImageFormat.Jpeg)
The problem is that i can select the folder that i want to output the images to with the "folderbrowserdialog" but it only saves the first picture. I have basically no idea on how to do this so if you could please make it clear on what to do that would be great thank you :) btw: i have already been looking all over the internet for this answer, i didn't just post straight away like some people do and wast your time. there is definitely nothing i can find on this thank you again :)
Re: Save an image from a picturebox control every 2 seconds that it updates...
First up, the PictureBox doesn't receive anything from the Timer. The Timer raises its Tick event and that's the sum total of its involvement. You can then put whatever code you want in the event handler but it's not the Timer executing that code, any more than its a ComboBox executing code in a SelectedIndexChanged event.
Also, the PictureBox is basically irrelevant too. You have to have the Image in the first place in order to display it in the PictureBox. It's the Image that you're saving, not the PictureBox, so you would save it the same way whether there is a PictureBox or not. There's not really much point going to the PictureBox first to get the Image when you must have already had the Image to begin with to put it in the PictureBox. Wherever you are assigning the Image object to the Image property of the PictureBox, which is presumably in the Tick event of the Timer, you already have a reference to the Image, so use that reference to call the Save method.
Also, the 'Call' keyword serves no purpose in this context.
So, your question is basically how to save multiple files with sequential file names. One answer is like this:
vb.net Code:
Dim fileNumber = 1
Dim filePath = IO.Path.Combine(folderPath, String.Format("ss{0}.jpeg"))
Do While IO.File.Exists(filePath)
fileNumber += 1
filePath = IO.Path.Combine(folderPath, String.Format("ss{0}.jpeg"))
Loop
myImage.Save(filePath, Imaging.ImageFormat.Jpeg)
Now, I'm not sure how many files there would have to be before you started to see a performance issue as a result of that code. A way to avoid having to test the same file names over and over again would be to modify that code like so:
vb.net Code:
Static fileNumber As Integer = 1
Dim filePath = IO.Path.Combine(folderPath, String.Format("ss{0}.jpeg"))
Do While IO.File.Exists(filePath)
fileNumber += 1
filePath = IO.Path.Combine(folderPath, String.Format("ss{0}.jpeg"))
Loop
myImage.Save(filePath, Imaging.ImageFormat.Jpeg)
fileNumber += 1
That will remember the file number between calls. Each time a file is saved the number is incremented and remembered. next time it needs to save a file it will start looking at that last remembered value rather than back at 1 each time. Assuming that the folder is empty to begin with and you never add any files any other way, File.Exists will never return True so you'll never waste any time trying to find the next available number.
Re: Save an image from a picturebox control every 2 seconds that it updates...
Hi thanks for your reply i still can't get it to work, i get an error when i try to capture it with a button that activates the timer so im just going to copy all my code from the timer and see what you think
vb Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'Dim strfilename As String
Dim folder As String
'strfilename = "1.jpeg"
folder = FolderBrowserDialog1.SelectedPath
Form3.screenshot.Image = CaptureImage(Form4.Location.X, Form4.Location.Y, Form4.ClientSize.Width, Form4.ClientSize.Height)
'Dim area As Rectangle
'Dim capture As System.Drawing.Bitmap
'Dim graph As Graphics
'area = Form4.Bounds
'capture = New System.Drawing.Bitmap(Bounds.Width, Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
'graph = Graphics.FromImage(capture)
'graph.CopyFromScreen(area.X, area.Y, 0, 0, area.Size, CopyPixelOperation.SourceCopy)
'Form3.screenshot.Image = capture
Static fileNumber As Integer = 1
Dim filePath = IO.Path.Combine(folder, String.Format("ss{0}.jpeg"))
Do While IO.File.Exists(filePath)
fileNumber += 1
filePath = IO.Path.Combine(folder, String.Format("ss{0}.jpeg"))
Loop
Form3.screenshot.Image.Save(filePath, Imaging.ImageFormat.Jpeg)
fileNumber += 1
'Call Form3.screenshot.Image.Save(folder & "\" & strfilename, System.Drawing.Imaging.ImageFormat.Jpeg)
'capture.Save("FolderBrowserDialog1.SelectedPath", System.Drawing.Imaging.ImageFormat.Bmp)
End Sub
Thanks :)
Re: Save an image from a picturebox control every 2 seconds that it updates...
What I think is that you should tell us what the error message is and where it occurred rather than just that an error occurred.