By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,214 Members | 2,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,214 IT Pros & Developers. It's quick & easy.

webcam problem

P: 43
I have made a picturebox and a coding witch takes a picture in the picturebox, but the problem is that I need to save it as a image file before I can use getpixel, so when I use a save picturebox as image then VB.NET returns an error with image not exists,
Jan 30 '08 #1
Share this Question
Share on Google+
11 Replies

Expert 5K+
P: 8,434
Can you show us what you've attempted so far, that doesn't work?
Jan 31 '08 #2

P: 180
I know what Patr's talking about, and I'd like to know how to do this too.
Debasisdas has made an example of how to capture from a webcam and "display the live video in a picturebox". However, what Windows is actually doing is displaying the picture on top of the picture box (known as overlaying). Therefore the picturebox actually still is empty, meaning that GetPixel() will fail to give back colours of the camera's images, and SavePicture() will obviously also fail to save the images.

We need a way to actually set the contents of the picturebox, and the easiest way seems to be by saving an image from the camera and loading it into the picturebox, where it can then be used as any other loaded image file. The downside to this is that the frame rate would probably be very low (probably no more than 4 fps or so) due to overheads from the file system (or possibly the hard disk itself, reading/writing the file over and over).

EDIT: I'm using VB6 and Windows XP, by the way, and this still happens. However, I tried using MCI to play a video in a picture box (this being related to webcams because MCI also just overlays in XP), on an extremely old computer running Windows 98, and GetPixel() worked - I think that may be because GetPixel() works differently on 98 compared to XP, rather than anything else.

EDIT2: Stupidly, it didn't occur to me at the time to try SavePicture(), which would confirm whether it was just GetPixel() being different on 98, or if 98 was actually placing the picture into the picturebox
Jan 31 '08 #3

P: 43
that is exactly what I am talking about.
Jan 31 '08 #4

Expert 5K+
P: 8,434
Thanks for the input, Robbie.

I don't know the answer, but it occurs to me there may be another workaround. Not a really elegant way to go, but possibly faster. That is, use the API call (I forget the details, this is probably going back over 5 years) which is typically used by screensavers to grab the desktop.

When you write a screensaver, typically the first thing you do (at least in the simple examples I coded, way back) is to grab a snapshot of the current display, so you can play with it. It should be possible either do this then throw away the part you don't want, or if possible, grab just the part where your picturebox lives.

If interested, you should be able to find plenty of examples by searching for info on VB and screen savers.
Jan 31 '08 #5

P: 1
Ok ok the answer is actually very simple. i made an instant messanger with webcam and voice about a year ago. for the image thing just get the handle of the picturebox.

camHwnd = capCreateCaptureWindow(CamWnd, 0, 0, 0, 320, 240, me.hwnd, 0)

Then all you have to do is save like this:

SavePicture Picture1.picture, Path

Then Reload the picture like this:

Picture1.picture = loadpicture(path )

thats is.

if thats what you were doing that should werk. im testing right now and after getting the picture reloaded into the picturebox i am able to use getpixel function on it.

P.S. the frame rate on my webcam is about twice as good as the frame rate of yahoo instant messanger's webcam. my computer specs. are: xp pro, AMD 64 moblie Athlon, 1gig ram. like i said the saving and opening of the image does slow the framerate but if you broad casting over the internet your fps will well over exceed your streaming speeds.
Feb 23 '08 #6

P: 4
Hi, I'm having the exact same problem. I am trying to detect certain pixels from a video streaming from a webcam (don't really need to detect the entire webcam image, just 10 or 20 pixels of it to see if they change).

But I can't get it working! I've followed this thread and this is the code I've ended up with:

This is the code in a module (.bas file):
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  3. Public Const ws_child As Long = &H40000000
  4. Public Const ws_visible As Long = &H10000000
  5. Global Const WM_USER = 1024
  6. Global Const wm_cap_driver_connect = WM_USER + 10
  7. Global Const wm_cap_set_preview = WM_USER + 50
  8. Global Const WM_CAP_SET_PREVIEWRATE = WM_USER + 52
  9. Global Const WM_CAP_DRIVER_DISCONNECT As Long = WM_USER + 11
  10. Public Const WM_CAP_DLG_VIDEOFORMAT As Long = WM_USER + 41
  11. Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  12. Declare Function capCreateCaptureWindow Lib "avicap32.dll" Alias "capCreateCaptureWindowA" (ByVal a As String, ByVal b As Long, ByVal c As Integer, ByVal d As Integer, ByVal e As Integer, ByVal f As Integer, ByVal g As Long, ByVal h As Integer) As Long

This is the code in my form:

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. 'General Declaration
  3. Dim hwdc As Long
  4. Dim startcap As Boolean
  6. Private Sub cmdCapture_Click()
  7. Dim temp As Long
  9.   hwdc = capCreateCaptureWindow("Debasis Das", ws_child Or ws_visible, 0, 0, 320, 240, Picture1.hWnd, 0)
  10.   If (hwdc <> 0) Then
  11.     temp = SendMessage(hwdc, wm_cap_driver_connect, 0, 0)
  12.     temp = SendMessage(hwdc, wm_cap_set_preview, 1, 0)
  13.     temp = SendMessage(hwdc, WM_CAP_SET_PREVIEWRATE, 30, 0)
  14.     startcap = True
  15.     Else
  16.     MsgBox ("No Webcam found")
  17.   End If
  18. End Sub
  20. Private Sub cmdClose_Click()
  21.   Dim temp As Long
  22.   If startcap = True Then
  23.   temp = SendMessage(hwdc, WM_CAP_DRIVER_DISCONNECT, 0&, 0&)
  24.   startcap = False
  25.   End If
  26. End Sub
  28. Private Sub cmdexit_Click()
  29.   Unload Me
  30. End Sub
  32. Private Sub cmdVideoFormat_Click()
  33.     Dim temp As Long
  34.     If startcap = True Then
  35.         temp = SendMessage(hwdc, WM_CAP_DLG_VIDEOFORMAT, 0&, 0&)
  36.     End If
  37. End Sub
  40. Private Sub Timer1_Timer()
  41.     Dim camwnd As String
  42.     Dim CamHwnd As String
  43.     Dim Path As String
  45.     camwnd = Me.hWnd
  46.     CamHwnd = capCreateCaptureWindow(camwnd, 0, 0, 0, 320, 240, Me.hWnd, 0)
  48.     Path = "C:\Test.jpg"
  50.     SavePicture Picture1.Picture, Path
  51.     Picture1.Picture = LoadPicture(Path)
  53.     Me.lblColor = Me.Picture1.Point(100, 100) 'Just a test, trying to get a pixel value
  54. End Sub

The idea is that the timer will periodically (several times per second) check some pixel (in this example at x=100, y=100) and for now just print the value of this pixel in a label in the form.

But the program crashes at the SavePicture line, saying "Invalid property value", and then VB6 itself crashes.

I think that we're really close here. I've tried to fill the "camwnd" variable with both the name and hWnd of both the form and the picturebox, but there's no difference in the outcome.

Does anyone know how I can get the value of certain pixels in the video stream (preferrably without having to save it to a file and load it again)?

Thanks. :-)
Feb 27 '08 #7

P: 4
Well, I just made the strangest breakthrough.

I removed the saving and loading of the image file, and replaced "Picture1.hwnd" with "Me.HWnd" in the capCreateCaptureWindow command at line 9 in my code, and... it works!!!

I have no idea why or how, but it works. The image is now drawn directly on the form's surface, and I can still use Me.Picture1.Point to get the value, which can then be split up in it's Red, Green and Blue components.

But I had to move the picturebox to position 0,0 (upper left corner in the form) and make it the same size as the picture from the webcam (320x240 pixels in my case). I also set the ScaleMode of both the form and picturebox to "Pixel".

I really don't have any idea why this works, but I don't really care as long as it does. :-)
Feb 27 '08 #8

P: 4
BTW, if anyone's interested, this is the sub I use to get Red, Green and Blue from the color value:

Expand|Select|Wrap|Line Numbers
  1. Public Function get_rgb(Color, rd, gr, bl)
  2.     Dim tmpColor
  3.     If Color < 0 Then Color = 0
  5.     tmpColor = Color / 65536
  6.     bl = Int(tmpColor)
  7.     tmpColor = (tmpColor - Int(tmpColor)) * 256
  9.     gr = Int(tmpColor)
  10.     tmpColor = (tmpColor - Int(tmpColor)) * 256
  12.     rd = Round(tmpColor)
  13. End Function
You put in the color you get from the webcam (Long data type), and three variables for Red, Green, and Blue (normally data type Byte). These variables will then be filled with RGB values.
Feb 27 '08 #9

Expert 5K+
P: 8,434
Thanks for sharing that with us, rfoshaug. I've had to split colours like this a few times, too. I'd like to make a couple of small comments about this version.
  • Ideally, you should work with Long data type wherever possible, for performance reasons. It might seem crazy, but it's actually slightly faster to work with Long than with Byte. (On a 32 bit system, that is. 64 bit may be different).
  • Integer division is faster, too. Try using \ instead of /.
  • You're doing the same calculation twice, unnecessarily. In line 7, for instance, you should use bl rather than Int(tmpColor).

This is all just nitpicking, of course - if the routine works, great. :)

EDIT: Oops! The integer division thing (\) might not work with your method.
Feb 28 '08 #10

P: 4
I've just discovered that these lines:

camwnd = Me.hWnd
CamHwnd = capCreateCaptureWindow(camwnd, 0, 0, 0, 320, 240, Me.hWnd, 0)

are not needed in the timer sub, and actually cause lockups and problems, so I'm removing them from the code in my earlier post.

Edit: It seems I can't edit that older post. Anyway, it's line 45 and 46 in the first code I posted, in the Timer sub.
Mar 1 '08 #11

Expert 5K+
P: 8,434
I've just discovered ....
I've edited it and put those two lines in italics. At least they will look slightly different, so when people get to this message they should know which lines you mean.

It might be better to just use the Reply function to copy the code from that message and re-post it here without those lines.
Mar 3 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.