dcsimg
Results 1 to 15 of 15

Thread: [Resolved] Webcam(have code) -> Picturebox

  1. #1

    Thread Starter
    Member intellilogic's Avatar
    Join Date
    Oct 2010
    Location
    Tyson's Corner, VA
    Posts
    39

    Question [Resolved] Webcam(have code) -> Picturebox

    First let me start with the fact that I am a hack of a programmer. I did some VB6 back in the day, I do PHP, but now I am using 2010. I am making a simple application to help me list some odd-ball stuff on ebay. (This takes a pictures of the item from the webcam for me.

    So I have this code to start and it works well:
    Code:
    ' This code is (c) 2009 Microsoft Corporation, written by Lucian Wischik.
    ' It is released under the Microsoft Public License, http://www.microsoft.com/opensource/licenses.mspx#Ms-PL
    
    
    Public Class Form1
        ' Note: WebCamLib.dll is x86 only, so we need to build this app for x86 architecture.
        Dim Touchless1 As New TouchlessLib.TouchlessMgr
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Eliminate flicker on repaint:
            SetStyle(ControlStyles.UserPaint, True)
            SetStyle(ControlStyles.AllPaintingInWmPaint, True)
            SetStyle(ControlStyles.DoubleBuffer, True)
            Me.UpdateStyles()
    
            ' Set up the webcam to fire an event when it captures a frame:
            If Touchless1.Cameras.Count = 0 Then MessageBox.Show("This application needs a webcam") : Return
            Touchless1.CurrentCamera = Touchless1.Cameras.First
            AddHandler Touchless1.CurrentCamera.OnImageCaptured, AddressOf Form1_Capture
    
            ' Give the user an opportunity to tweak the camera settings:
            'Touchless1.CurrentCamera.ShowPropertiesDialog(Me.Handle)
        End Sub
    
        Private Sub Form1_Capture()
            ' This event runs in a worker thread owned by Touchless. We want to handle the event
            ' in the main UI thread, so we use BeginInvoke. Note: this is still fine even if the
            ' form has been disposed before our invoke ends up happening.
            Me.BeginInvoke(New EventHandler(AddressOf Me.Invalidate))
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            Dim b = Touchless1.CurrentCamera.GetCurrentImage
            If b IsNot Nothing Then e.Graphics.DrawImage(b, 0, 0, Width, Height)
        End Sub
    End Class
    Ok, so I want the capture to be running in a PictureBox, I guess it's currently "painting" on the background of the form. I want to be able to save the image capture from the picture box to a file, preferably a PNG file. I also want to be able to crop the picture before saving, possibly right from the live capture? I think that's something I could start looking into once I have this thing is a damn picture box. Should you be so kind as to help with the code to save the image, I should ask that it's saved without a dialog box, the program will assign it's location and name via other information on the form. If it could be a function, that would be best.

    I am going to have it that you click a button for what you are taking a picture of and it saves accordingly (Item 001, Front|Back|Box...) Item number will be the folder and the view will be the file name, but I can do all this myself, just trying to explain why I want the save function to be the way I do.
    Last edited by intellilogic; Oct 28th, 2010 at 06:23 PM. Reason: Resolved

  2. #2

    Thread Starter
    Member intellilogic's Avatar
    Join Date
    Oct 2010
    Location
    Tyson's Corner, VA
    Posts
    39

    Re: Webcam(have code) -> Picturebox

    *bump* please help.

  3. #3

    Thread Starter
    Member intellilogic's Avatar
    Join Date
    Oct 2010
    Location
    Tyson's Corner, VA
    Posts
    39

    Re: Webcam(have code) -> Picturebox

    Well, I kinda got it. It worked for a bit, now I am getting this error:

    Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.
    on my line with the picturebox = touchless...

    Code:
    ' This code is (c) 2009 Microsoft Corporation, written by Lucian Wischik.
    ' It is released under the Microsoft Public License, http://www.microsoft.com/opensource/licenses.mspx#Ms-PL
    
    Public Class Form1
        ' Note: WebCamLib.dll is x86 only, so we need to build this app for x86 architecture.
        Dim Touchless1 As New TouchlessLib.TouchlessMgr
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Eliminate flicker on repaint:
            SetStyle(ControlStyles.UserPaint, True)
            SetStyle(ControlStyles.AllPaintingInWmPaint, True)
            SetStyle(ControlStyles.DoubleBuffer, True)
            Me.UpdateStyles()
    
            ' Set up the webcam to fire an event when it captures a frame:
            If Touchless1.Cameras.Count = 0 Then MessageBox.Show("This application needs a webcam") : Return
            Touchless1.CurrentCamera = Touchless1.Cameras.First
            AddHandler Touchless1.CurrentCamera.OnImageCaptured, AddressOf Form1_Capture
    
            ' Give the user an opportunity to tweak the camera settings:
            'Touchless1.CurrentCamera.ShowPropertiesDialog(Me.Handle)
        End Sub
    
        Private Sub Form1_Capture()
            ' This event runs in a worker thread owned by Touchless. We want to handle the event
            ' in the main UI thread, so we use BeginInvoke. Note: this is still fine even if the
            ' form has been disposed before our invoke ends up happening.
            Me.BeginInvoke(New EventHandler(AddressOf Me.Invalidate))
    
            PictureBox1.Image = Touchless1.CurrentCamera.GetCurrentImage()
        End Sub
    End Class
    Last edited by intellilogic; Oct 27th, 2010 at 02:35 AM.

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,099

    Re: Webcam(have code) -> Picturebox (NOT Resolved, almost)

    Hi intellilogic,

    Assuming the rest of your code is correct, it's possible that what you need to do is to transfer the line which is causing the threading error:

    Code:
    PictureBox1.Image = Touchless1.CurrentCamera.GetCurrentImage()
    to the PictureBox1.Invalidate sub. You can select that event from the drop down boxes at the top of the code file in Visual Studio.

    Then you change the Invalidate line like this:

    Code:
    Me.BeginInvoke(New EventHandler(AddressOf PictureBox1.Invalidate))
    BB

  5. #5

    Thread Starter
    Member intellilogic's Avatar
    Join Date
    Oct 2010
    Location
    Tyson's Corner, VA
    Posts
    39

    Re: Webcam(have code) -> Picturebox (NOT Resolved, almost)

    That works. Anyway I can put this all in one function that is called when form load. So when I use the code in other projects, it's easier to use.

    From_Load
    WebCam()
    End...


    WebCam()
    Everything
    End...

  6. #6

  7. #7

    Thread Starter
    Member intellilogic's Avatar
    Join Date
    Oct 2010
    Location
    Tyson's Corner, VA
    Posts
    39

    Re: Webcam(have code) -> Picturebox (NOT Resolved, almost)

    Well, I was trying to slim it down into a function, so the code is out of form load / print
    One nice function that I can call maybe with the picturebox name as a variable

    CaptureWebCam(PictureBoxName1)

  8. #8
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,099

    Re: Webcam(have code) -> Picturebox (NOT Resolved, almost)

    You can tidy up your Form1_Load sub by getting rid of the first five lines. They are no longer needed because a PictureBox is double buffered by default. (Besides they could have been replaced by the single line Me.DoubleBuffered = True.)

    One way I would try out to package it would be to define a class that inherits from pictureBox. Let's say you call it a TouchlessViewer. Here's a sketch of what it might be like, but I don't have Touchless webcam or the time to try it out so don't throw away your present version.

    Code:
    Public Class TouchlessViewer
    Inherits PictureBox
    
    Dim Touchless1 As New Touchless '...whatever it was
    
    Public Sub New
    'transfer the stuff from your present Form Load to here. Change the Addressof Form_Capture to Addressof Capture, just for tidiness.
    End Sub
    
    Private Sub Capture
     'Code from your present Form1_Capture sub
    End Sub
    
    Protected Overrides Sub OnInvalidate('...VS fills in the rest)
       'Code from your present PictureBox1 invalidate.
       MyBase.OnInvalidate
    End Sub
    
    End Class
    You build the project, and a new TouchlessViewer tool appears in the toolbox. Then you can use it like a PictureBox on any form, and (if all is well) it will show pictures from your webcam too.

    BB

  9. #9

    Thread Starter
    Member intellilogic's Avatar
    Join Date
    Oct 2010
    Location
    Tyson's Corner, VA
    Posts
    39

    Re: Webcam(have code) -> Picturebox (NOT Resolved, almost)

    Ok, here's what I got and it's sexy

    Code:
    Public Class Form1
        Dim Touchless1 As New TouchlessLib.TouchlessMgr
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            StartCapture()
        End Sub
    
        Public Sub StartCapture()
            If Touchless1.Cameras.Count = 0 Then MessageBox.Show("This application needs a webcam") : Return
            Touchless1.CurrentCamera = Touchless1.Cameras.First
            AddHandler Touchless1.CurrentCamera.OnImageCaptured, AddressOf CaptureInvoke
        End Sub
    
        Private Sub CaptureInvoke()
            PictureBox1.Image = Touchless1.CurrentCamera.GetCurrentImage()
        End Sub
    
    End Class
    Yea, much better, much easier to understand, less BS, thanks so much
    Last edited by intellilogic; Oct 29th, 2010 at 03:45 PM. Reason: Removed override

  10. #10

    Thread Starter
    Member intellilogic's Avatar
    Join Date
    Oct 2010
    Location
    Tyson's Corner, VA
    Posts
    39

    Re: [Resolved] Webcam(have code) -> Picturebox

    What does that Overrides Sub do? I took it out now that I have changed it around, it runs...? I don't really understand what I am doing here, heh.

  11. #11
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,099

    Re: [Resolved] Webcam(have code) -> Picturebox

    Yes the code looks a lot nicer now. What is more, if it's working the way you want, you can even get rid of that OnInvalidated sub, because it only has the standard MyBase line in it and no code of your own.

    Still I don't understand what you are doing either. You seem to be much cleverer than you thought, because you have tidied it up much more drastically than I suggested! You bypassed the Me.BeginInvoke method for calling the Invalidated sub. I was wondering about that. Was it really just an unnecessary complication? Maybe it will turn out to be needed in some special situation, and your pet hamster may explode if you don't use it. But if you are just starting out I would say don't worry.

    BB



    Explanation: OnInvalidated is practically the same thing as an event handler like Form1_Invalidate. Nearly all (or maybe all) event handlers have a On.. sub equivalent. For some reason the On subs are preferred when you inherit from a control, such as in the Inherited PictureBox I suggested.

  12. #12

    Thread Starter
    Member intellilogic's Avatar
    Join Date
    Oct 2010
    Location
    Tyson's Corner, VA
    Posts
    39

    Re: [Resolved] Webcam(have code) -> Picturebox

    I come from a VB6 background and don't really know anything about event handlers =-0 The touchless thing is some microsoft thing, I don't really know what it is, but it works with every web cam I've plugged in thus far (A few logitech ones and a MS one). Thanks again for the help, I really do appreciate it.

    Short version of the story on my being clever. I used to be a developer, I was also in the Army reserve(Joined pre-911), got called, went to Iraq, got blow up. Now I have TBI(Traumatic Brain Injury) as well as other issues, I have lost higher learning, as in, I have had to re-learn how to code; but for some reason I haven't lost certain fundamentals of programing. So what I know and don't know seam illogical to some before they understand I have TBI. Things like: I know how to get somewhere still, but no longer remember any of the road names, why I know the route, etc...

  13. #13
    New Member
    Join Date
    Aug 2012
    Posts
    2

    Re: [Resolved] Webcam(have code) -> Picturebox

    hi there, is there anybody here? would like to know, i'm using VB2010 on 34 bit OS, but i couldn't add Webcamlib.Dll to the project reference, do anybody know how to solve this? i have downloaded the reference from touchless website. thanks.

  14. #14
    New Member
    Join Date
    Aug 2012
    Posts
    2

    Re: [Resolved] Webcam(have code) -> Picturebox

    guess that there's nobody around

  15. #15

    Re: [Resolved] Webcam(have code) -> Picturebox

    This thread was two years old. I recommend you make a new thread which clearly states your problem rather than unearthing a two year old thread.

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