AVICAP32 and VIDEOHDR to Bitmap

D

Daniel Friend

Hello,

I have a question. I would like to know how I can convert the Video Header
to a bitmap in VB .NET

I searched everywhere and could not see how to do this. This is as far as I
get. I am getting the cam preview to work and it is calling the
FrameCallbackTarget function just fine... I just need to make the stream
frame a bitmap.

Any help would be greatly appreciated!!!!

Thanks,

Dan


Here is my code:

Delegate Function CallBackDelegate(ByVal hwnd As IntPtr, ByRef lpVHdr As
VIDEOHDR) As IntPtr
Public delCallBack As CallBackDelegate = New CallBackDelegate(AddressOf
FrameCallbackTarget)
Public Declare Function SendMessage2 Lib "user32.dll" Alias "SendMessageA"
(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal
lParam As CallBackDelegate) As IntPtr

Structure VIDEOHDR
Dim lpData As Integer '// address of video buffer
Dim dwBufferLength As Integer '// size, in bytes, of the Data buffer
Dim dwBytesUsed As Integer '// see below
Dim dwTimeCaptured As Integer '// see below
Dim dwUser As Integer '// user-specific data
Dim dwFlags As Integer '// see below
<VBFixedArray(3)> Dim dwReserved() As Integer '// reserved; do not use}
End Structure

Private Sub StartCam()
Dim iHeight As Integer = PictureBox1.Height
Dim iWidth As Integer = PictureBox1.Width
hHwnd = capCreateCaptureWindowA(iDevice.ToString, WS_VISIBLE Or
WS_CHILD, 0, 0, 320, 240, PictureBox1.Handle.ToInt32, 0)
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, PictureBox1.Width,
PictureBox1.Height, SWP_NOMOVE Or SWP_NOZORDER)
SendMessage2(New IntPtr(hHwnd), WM_CAP_SET_CALLBACK_FRAME, New
IntPtr(0), delCallBack)
Else
DestroyWindow(hHwnd)
End If
end sub

Public Function FrameCallbackTarget(ByVal hwnd As IntPtr, ByRef lpVHdr As
VIDEOHDR) As IntPtr
Dim oBMP As New Bitmap(640, 480, 640 * 3,
Imaging.PixelFormat.Format24bppRgb, New System.IntPtr(lpVHdr.lpData))
PictureBox2.Image = oBMP
End Function
 
B

Bart Mermuys

Hi,

Daniel Friend said:
Hello,

I have a question. I would like to know how I can convert the Video
Header to a bitmap in VB .NET

I searched everywhere and could not see how to do this. This is as far as
I get. I am getting the cam preview to work and it is calling the
FrameCallbackTarget function just fine... I just need to make the stream
frame a bitmap.

Any help would be greatly appreciated!!!!
Thanks,

Dan


Here is my code:

Delegate Function CallBackDelegate(ByVal hwnd As IntPtr, ByRef lpVHdr As
VIDEOHDR) As IntPtr
Public delCallBack As CallBackDelegate = New CallBackDelegate(AddressOf
FrameCallbackTarget)
Public Declare Function SendMessage2 Lib "user32.dll" Alias "SendMessageA"
(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal
lParam As CallBackDelegate) As IntPtr

Structure VIDEOHDR
Dim lpData As Integer '// address of video buffer
Dim dwBufferLength As Integer '// size, in bytes, of the Data buffer
Dim dwBytesUsed As Integer '// see below
Dim dwTimeCaptured As Integer '// see below
Dim dwUser As Integer '// user-specific data
Dim dwFlags As Integer '// see below
<VBFixedArray(3)> Dim dwReserved() As Integer '// reserved; do not use}
End Structure

Private Sub StartCam()
Dim iHeight As Integer = PictureBox1.Height
Dim iWidth As Integer = PictureBox1.Width
hHwnd = capCreateCaptureWindowA(iDevice.ToString, WS_VISIBLE Or
WS_CHILD, 0, 0, 320, 240, PictureBox1.Handle.ToInt32, 0)
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, PictureBox1.Width,
PictureBox1.Height, SWP_NOMOVE Or SWP_NOZORDER)
SendMessage2(New IntPtr(hHwnd), WM_CAP_SET_CALLBACK_FRAME, New
IntPtr(0), delCallBack)
Else
DestroyWindow(hHwnd)
End If
end sub

Public Function FrameCallbackTarget(ByVal hwnd As IntPtr, ByRef lpVHdr As
VIDEOHDR) As IntPtr
Dim oBMP As New Bitmap(640, 480, 640 * 3,
Imaging.PixelFormat.Format24bppRgb, New System.IntPtr(lpVHdr.lpData))
PictureBox2.Image = oBMP
End Function

Your code seems to work, what is the problem you have with the Bitmap ? You
may want to check if
lpVHdr.dwBufferLength == stride * height .

And a warning which i'm sure isn't causing your problem now but anyway you
should always use IntPtr inside a function sign or structure where a pointer
or handle was used unmanaged (except for function pointers where you may use
a delegate).

HTH,
Greetings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top