473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

webcam problem

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
11 3764
Killer42
8,435 Expert 8TB
Can you show us what you've attempted so far, that doesn't work?
Jan 31 '08 #2
Robbie
180 100+
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
that is exactly what I am talking about.
Jan 31 '08 #4
Killer42
8,435 Expert 8TB
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
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
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
  2.  
  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
  13.  


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
  5.  
  6. Private Sub cmdCapture_Click()
  7. Dim temp As Long
  8.  
  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
  19.  
  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
  27.  
  28. Private Sub cmdexit_Click()
  29.   Unload Me
  30. End Sub
  31.  
  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
  38.  
  39.  
  40. Private Sub Timer1_Timer()
  41.     Dim camwnd As String
  42.     Dim CamHwnd As String
  43.     Dim Path As String
  44.  
  45.     camwnd = Me.hWnd
  46.     CamHwnd = capCreateCaptureWindow(camwnd, 0, 0, 0, 320, 240, Me.hWnd, 0)
  47.  
  48.     Path = "C:\Test.jpg"
  49.  
  50.     SavePicture Picture1.Picture, Path
  51.     Picture1.Picture = LoadPicture(Path)
  52.  
  53.     Me.lblColor = Me.Picture1.Point(100, 100) 'Just a test, trying to get a pixel value
  54. End Sub
  55.  

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
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
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
  4.  
  5.     tmpColor = Color / 65536
  6.     bl = Int(tmpColor)
  7.     tmpColor = (tmpColor - Int(tmpColor)) * 256
  8.  
  9.     gr = Int(tmpColor)
  10.     tmpColor = (tmpColor - Int(tmpColor)) * 256
  11.  
  12.     rd = Round(tmpColor)
  13. End Function
  14.  
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
Killer42
8,435 Expert 8TB
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
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
Killer42
8,435 Expert 8TB
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

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

Similar topics

1
by: Kelowna | last post by:
Yes... Its a server os... i kno! i hav xp yea but tis system is for screwing with.. Im sure theres alot of ppl here tat kno tis stuff inside-out! im doing this for educational purposes so dont give...
4
by: Joakim Rylander | last post by:
Hi all, I need to create a public booth where people can look at themselves in a webcam and snap a picture which will be seen on a webpage. The backend is easy, it's the webcam part that I have...
0
by: Elp | last post by:
Hi, We have developped an client application in C#. Among other things, this application should display in 4 different windows (or 4 different panels in the same window), 4 webcam streams (web...
4
by: Mathieu Chavoutier | last post by:
Hi. Is it possible to do a program that show the "film" that is currently on our webcam ? I do not have a Webcam for the moment, so, if you have advices to chose a Webcam compliant (with what...
0
by: Steve | last post by:
Hi Can anyone please tell me how to use a webcam on a form. I have done an app that uses a webcam, but I have used the drivers for that webcam. Now I need to use a different camera on a...
2
by: Pat Mac | last post by:
Hopefully this is a good group to ask this question. I'm trying to programmatically control a micro innovations IC50C webcam with VB.NET. Specifically I'm using the Kodak scanner control. My...
3
by: DillonCzerny | last post by:
Hello Many times I see a lot of people post webcam videos on google video or youtube. I wonder how do they record it from those people’s webcam? I have a fake webcam and I want to record some...
13
by: Berco Beute | last post by:
I've been trying to access my webcam using Python, but I failed miserably. The camera works fine under Ubuntu (using camora and skype), but I am unable to get WebCamSpy or libfg to access my...
5
by: frakie | last post by:
Hi there, I need a simple and stupid few line source code to access my webcam. I found lots of huge projects (c++ c and other) which are so big that they are impossible to be used. Does somebody...
1
by: Steve | last post by:
Hi All I have a VB.net 2005 windows forms application which uses webcam to take photos of staff members etc for use within the application All works fine if an external webcam is used The...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.