capture video in vb.net, want help

can you explain this code?

Page 1 of 1

9 Replies - 7101 Views - Last Post: 15 November 2009 - 04:18 AM Rate Topic: -----

#1 alsayeda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-March 09

capture video in vb.net, want help

Posted 02 November 2009 - 11:57 PM

hello,

i have a project "motion detection"
and i try capture video from webcam to vb.net
i have along code for that
but i want understand every line
some of code i not understand it
can anybody explain this code to me

thanks,

Const WM_CAP As Short = &H400S

	Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
	Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
	Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30

	Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
	Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
	Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
	Const WS_CHILD As Integer = &H40000000
	Const WS_VISIBLE As Integer = &H10000000
	Const SWP_NOMOVE As Short = &H2S
	Const SWP_NOSIZE As Short = 1
	Const SWP_NOZORDER As Short = &H4S
	Const HWND_BOTTOM As Short = 1

	Dim iDevice As Integer = 0 ' Current device ID
	Dim hHwnd As Integer ' Handle to preview window

	Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
		(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
		<MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer

	Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Integer, _
		ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, _
		ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer

	Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean

	Declare Function capCreateCaptureWindowA Lib "avicap32.dll" _
		(ByVal lpszWindowName As String, ByVal dwStyle As Integer, _
		ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, _
		ByVal nHeight As Short, ByVal hWndParent As Integer, _
		ByVal nID As Integer) As Integer

	Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, _
		ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, _
		ByVal cbVer As Integer) As Boolean



Is This A Good Question/Topic? 0
  • +

Replies To: capture video in vb.net, want help

#2 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: capture video in vb.net, want help

Posted 03 November 2009 - 04:59 AM

Hi,

What you are seeing here are called API functions.

They need to be declared in order for you to use them.

API's are windows functions

in this line of code

 Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
		(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
		<MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer




you are linking to the user32.dll.

And the Function is SendMessage with an alias of SendMessageA

;)
Was This Post Helpful? 1
  • +
  • -

#3 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: capture video in vb.net, want help

Posted 03 November 2009 - 07:00 AM

It probably won't work as it is modified vb6 code, and has several mistakes.

Windows provides dll files containing functions that you can call from your code (user code) to ask windows to do things. So, say you want the sound volume to increase - you call a standard, well documented, function in a dll provided by windows to set a volume. These functions are written in C, so your VB.Net code that calls them has to be rather special. If you were calling a .Net dll then it would be much simpler. The technology built into the .Net framework to allow you to call C functions is called platform invoke ("platform" = windows, invoke = "call a method"). Most people just call them API calls.

You start by looking at the C function in the MSDN library or C header file. You have to declare a .Net version of the function. It will be a stub function - it will contain no code. When your code calls the function, the .Net framework will then call the C dll for you, translating arguments and return types into the bits that C will understand. Translating the arguments is called Marshalling. It is a complicated topic, for every C parameter there are usually a number of ways to declare it in the .Net function. You have to consider:
  • Type: The C type should have some .Net equivalent. So if the C function wants a LONG, which is a signed 32 bit integer, you use Integer. The msdn topic "platform invoke data types" helps here. Some C types are custom types, for example structures. For these you need to declare a .Net structure that is bit-for-bit compatible with the C function. This is complex.
  • Direction: Is the parameter going in to the function, coming out from the function or both. In C you have IN, OUT and IN/OUT in .Net you have ByVal and ByRef (and some attributes, but ignore that...).
  • Marshalling Attributes: For some parameters, there are many different C types that could match the .Net type. For example in C you have ANSI strings, UNICODE strings, ANSI char array, UNICODE char arrays and more, but in .Net it is just String. So when you send the String to an API function you might need to tell .Net which sort of C string it is. This can be done with marshalling attributes such as <MarshalAs(UnmanagedType.LPStr)>.
When you consider what the framework needs to know to call a function in a dll...
1) It needs to know where the dll is.
2) It needs to know the name of the funciton.
3) It needs to know the signature of the function (there may be overrides).

You also need some way to tell the .Net framework that the function that you are declaring is not a normal function...

Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
		(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
		<MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer


Declare is saying "This is not a normal function, we are calling a function in an external library".
SendMessage is the name of the function. Windows declares two versions in the dll though. It has one called SendMessageA and another called SendMessageW. The one with an A is ANSI - it accepts ASCII characters. The one with a W is UNICODE (Wide strings), all strings and chars are unicode. The syntax Alias "SendMessageA" means that in the .Net code we will call SendMessage, but the .Net framework should call the SendMessageA function in the dll.

The parameter list demonstrates some of the things I talked about, but the parameters are incorrect. People often find vb6 code and translate it into vb.net code with simple substitution rules. This doesn't work. The vb6 was based on the C code, the vb.net code should be based on the C code.
BAD: C code -> vb6 code -> VB.Net code
GOOD: C code -> VB.Net code.

So, let's see what is wrong. First we need to find the C version of the signature. It's in the msdn library.

LRESULT SendMessage(
HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam);


For hWnd the signature has the type HWND. If you look up HWND on the msdn "windows data types" page then you'll see HWND is the same as HANDLE. If you look up HANDLE in the msdn "platform invoke data types" the you'll see that for HANDLE you should use IntPtr. So, instead of Integer, it should use IntPtr. This is important. An Integer is a signed 32 bit value. A HWND is just a pointer, pointers differ in size depending on the platform. On a 64 bit OS in a 64 bit program the pointers are 64 bits in size. If you use an Int32 then it is not the correct size. An IntPtr will vary in size accordingly.

Msg is a UINT, so to be pedantic you could use a UInteger, but it's usually not worth the bother, so Integer is ok.

The WPARAM data type in the "windows data types" document is described as a UINT_PTR - it's another pointer value which will vary in size depending on the platform. How you marshal it depends on how you are going to use it, you can use it to send Integers, or Structures, or Strings... IntPtr would be the standard approach.

LPARAM is a LONG_PTR. Again, that's a pointer so it will vary in size depending on the platform. Marshalling it as Object with an AsAny Attribute is completely wrong - it's an attempt to translate from vb6 and it makes no sense in .Net. IntPtr should be used.

LRESULT is in the "windows data types" as another UINT_PTR, so that should be IntPtr too.

So, a basic version of SendMesasge might be:

Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
		(ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, _
		ByVal lParam As IntPtr) As IntPtr


Usually though, you declare it slightly differently, to fit the purpose to which you are using it. There's a wiki - www.pinvoke.net which has VB.Net signatures for many API functions. Use it with caution though - it is a wiki.

For constants, you usually have to find the appropriate C header file to find the definition. A simple technique is to just google it, for example search google for

"#Define WM_CAP_DRIVER_CONNECT"

will usually find the appropriate header file defining the constant.

This post has been edited by crepitus: 03 November 2009 - 07:02 AM

Was This Post Helpful? 1
  • +
  • -

#4 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: capture video in vb.net, want help

Posted 03 November 2009 - 07:37 AM

Hey Crepitus,

Maybe convert this to a Tutorial aswell.... Nicely done!
Was This Post Helpful? 0
  • +
  • -

#5 alsayeda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-March 09

Re: capture video in vb.net, want help

Posted 04 November 2009 - 10:26 PM

thanks motcom and crepitus
your replies very helpful

I check the code it work in some university computers , but in my laptop doesn't work

i check it with same webcam
are you know why?

regards,
Was This Post Helpful? 0
  • +
  • -

#6 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: capture video in vb.net, want help

Posted 04 November 2009 - 11:07 PM

Hi,

Where does it not work? Does it give you any errors in the code itself?
Was This Post Helpful? 0
  • +
  • -

#7 alsayeda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-March 09

Re: capture video in vb.net, want help

Posted 09 November 2009 - 02:30 AM

no error on code
but no video

in my laptop and in some another computers it give me a black screen
but with other comuters it show me the video and save some pictures if i want
Was This Post Helpful? 0
  • +
  • -

#8 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: capture video in vb.net, want help

Posted 09 November 2009 - 02:46 AM

Are you running a 64 bit version of windows? The api declarations are not correct in your post, and that could be the problem. Or, maybe your webcam doesn't have the driver required to get it to work.

Could you show all the code? You aren't showing the parts where you create the capture window and connect to the camera.

Have you debugged it? Step through the code and see which API functions are returning success results.

This post has been edited by crepitus: 09 November 2009 - 02:47 AM

Was This Post Helpful? 0
  • +
  • -

#9 alsayeda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-March 09

Re: capture video in vb.net, want help

Posted 14 November 2009 - 10:45 PM

i get this code from this site
http://www.vb-helper...eo_capture.html
Was This Post Helpful? 0
  • +
  • -

#10 EndLessMind  Icon User is offline

  • Android Expert
  • member icon

Reputation: 272
  • View blog
  • Posts: 1,250
  • Joined: 13-March 09

Re: capture video in vb.net, want help

Posted 15 November 2009 - 04:18 AM

Is there any way change the code to play a video insted?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1