PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 Save an image from a picturebox control every 2 seconds that it updates...-VBForums
Results 1 to 4 of 4

Thread: Save an image from a picturebox control every 2 seconds that it updates...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    29

    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

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,581

    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:
    1. Dim fileNumber = 1
    2. Dim filePath = IO.Path.Combine(folderPath, String.Format("ss{0}.jpeg"))
    3.  
    4. Do While IO.File.Exists(filePath)
    5.     fileNumber += 1
    6.     filePath = IO.Path.Combine(folderPath, String.Format("ss{0}.jpeg"))
    7. Loop
    8.  
    9. 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:
    1. Static fileNumber As Integer = 1
    2. Dim filePath = IO.Path.Combine(folderPath, String.Format("ss{0}.jpeg"))
    3.  
    4. Do While IO.File.Exists(filePath)
    5.     fileNumber += 1
    6.     filePath = IO.Path.Combine(folderPath, String.Format("ss{0}.jpeg"))
    7. Loop
    8.  
    9. myImage.Save(filePath, Imaging.ImageFormat.Jpeg)
    10. 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.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    29

    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:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    2.    
    3.         'Dim strfilename As String
    4.         Dim folder As String
    5.         'strfilename = "1.jpeg"
    6.         folder = FolderBrowserDialog1.SelectedPath
    7.        
    8.  
    9.         Form3.screenshot.Image = CaptureImage(Form4.Location.X, Form4.Location.Y, Form4.ClientSize.Width, Form4.ClientSize.Height)
    10.  
    11.         'Dim area As Rectangle
    12.         'Dim capture As System.Drawing.Bitmap
    13.         'Dim graph As Graphics
    14.  
    15.         'area = Form4.Bounds
    16.         'capture = New System.Drawing.Bitmap(Bounds.Width, Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
    17.         'graph = Graphics.FromImage(capture)
    18.  
    19.         'graph.CopyFromScreen(area.X, area.Y, 0, 0, area.Size, CopyPixelOperation.SourceCopy)
    20.         'Form3.screenshot.Image = capture
    21.  
    22.         Static fileNumber As Integer = 1
    23.         Dim filePath = IO.Path.Combine(folder, String.Format("ss{0}.jpeg"))
    24.         Do While IO.File.Exists(filePath)
    25.             fileNumber += 1
    26.             filePath = IO.Path.Combine(folder, String.Format("ss{0}.jpeg"))
    27.         Loop
    28.         Form3.screenshot.Image.Save(filePath, Imaging.ImageFormat.Jpeg)
    29.         fileNumber += 1
    30.  
    31.         'Call Form3.screenshot.Image.Save(folder & "\" & strfilename,   System.Drawing.Imaging.ImageFormat.Jpeg)
    32.         'capture.Save("FolderBrowserDialog1.SelectedPath", System.Drawing.Imaging.ImageFormat.Bmp)
    33.     End Sub

    Thanks

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,581

    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.

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
  •  



Featured


Click Here to Expand Forum to Full Width