From: Basha on
Hi,

What are the classes your using in WebCam Video capture Web application. How to handle PictureBox in C#.net Web Application. It seems PictureBox class is not there in Web Application.

Could you please provide me a sample C#.net Web Code.

Thank You.

Regards,
Basha




djamila wrote:

WebCam Highest resolution in a picture Box
06-Jun-08

Hello,


I wrote a program in C# that displays a webcam capture in a picture
box. I used windows API for this task. The program works well but the
picture of the webcam capture in my program is not as clear as that of
the webcam viewer.

I need to have a very good resolution from my webcam. I bought the
webcam that I think it has the clearest view and the highest
resolution (Logitech QuickCam Pro 9000). I thought the first time that
the problem is in my webcam device, but after a long search I realized
that the problem is in my C# program..

Here is a summary of the program that I wrote. Please tell me which
part of the program has to be changed in order to have a better view
of my webcam.


public class WebCamCapture
{

[DllImport("user32.dll", EntryPoint = "SendMessageA")]
public static extern bool SendMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
CAPSTATUS lParam // second message parameter
);

[DllImport("user32.dll", EntryPoint = "SendMessageA")]
public static extern bool SendMessage(
int hWnd, // handle to destination window
int Msg, // message
bool wParam, // first message parameter
int lParam // second message parameter
);

[DllImport("user32.dll", EntryPoint = "SendMessageA")]
public static extern bool SendMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
int lParam // second message parameter
);

[DllImport("user32.dll")]
public static extern int SetWindowPos(
int hWnd,
int hWndInsertAfter,
int x,
int y,
int cx,
int cy,
int wFlags
);

[DllImport("user32.dll")]
public static extern bool SetWindowPos(int hndw);


public struct POINTAPI
{
int x;
int y;
}

[DllImport("user32.dll")]
public static extern bool DestroyWindow(int hWnd);

public struct CAPSTATUS
{
public int uiImageWidth; // Width of
the image
public int uiImageHeight; // Height of
the image
int fLiveWindow; // Now Previewing
video?
int fOverlayWindow; // Now Overlaying
video?
int fScale; // Scale image to
client?
POINTAPI ptScroll; // Scroll position
int fUsingDefaultPalette; // Using default driver
palette?
int fAudioHardware; // Audio hardware
present?
int fCapFileExists; // Does capture file
exist?
int dwCurrentVideoFrame; // # of video frames
cap'td
int dwCurrentVideoFramesDropped; // # of video frames
dropped
int dwCurrentWaveSamples; // # of wave samples
cap'td
int dwCurrentTimeElapsedMS; // Elapsed capture
duration
int hPalCurrent; // Current palette in
use
int fCapturingNow; // Capture in
progress?
int dwReturn; // Error value after any
operation
int wNumVideoAllocated; // Actual number of
video buffers
int wNumAudioAllocated; // Actual number of
audio buffers
}


[DllImport("avicap32.dll")]
public static extern int capCreateCaptureWindowA(
string lpszWindowName,
int dwStyle,
int x,
int y,
int nWidth,
short nHeight,
int hWndParent,
int nID
);

[DllImport("avicap32.dll")]
public static extern bool capGetDriverDescriptionA(
short wDriver,
string lpszName,
int cbName,
string lpszVer,
int cbVer
);

[DllImport("kernel32.Dll")]
public static extern int DeleteFile(string lpPathName);

}


And here the program that displays my webcam capture in a picture box.


bReturn = WebCamCapture.capGetDriverDescriptionA(x, strName, 100,
strVer, 100);
OpenPreviewWindow();
WebCamCapture.CAPSTATUS s = new WebCamCapture.CAPSTATUS();
bReturn = WebCamCapture.SendMessage(hHwnd, WM_CAP_GET_STATUS,
Marshal.SizeOf(s), s);
Debug.WriteLine(String.Format("Video Size {0} x {1}", s.uiImageWidth,
s.uiImageHeight));



private void OpenPreviewWindow()
{
int iHeight = this.pictureBox.Height;
int iWidth = this. pictureBox.Width;

// Open Preview window in picturebox
hHwnd = WebCamCapture.capCreateCaptureWindowA(iDevice.ToString(),
WS_VISIBLE | WS_CHILD, 0, 0, 1280, 1024, this.
pictureBox.Handle.ToInt32(), 0);

// Connect to device
if (WebCamCapture.SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice,
0))
{
// Set the preview scale
WebCamCapture.SendMessage(hHwnd, WM_CAP_SET_SCALE, true, 0);

//Set the preview rate in milliseconds
WebCamCapture.SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0);

//Start previewing the image from the camera
WebCamCapture.SendMessage(hHwnd, WM_CAP_SET_PREVIEW, true, 0);

// Resize window to fit in picturebox
WebCamCapture.SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, this.
pictureBox.Width, this. pictureBox.Height, SWP_NOMOVE | SWP_NOZORDER);
}
else
{
// Error connecting to device close window
hHwnd = 0;
}
}

Previous Posts In This Thread:

On Thursday, June 05, 2008 4:03 PM
Peter Duniho wrote:

Re: WebCam Highest resolution in a picture Box
On Thu, 05 Jun 2008 12:41:20 -0700, djamila <djamilabouzid(a)gmail.com>
wrote:


I don't really understand why you're posting here. As near as I can tell
from the code you posted, using a PictureBox is pointless, because all you
use it for is the dimensions. You might as well just have a generic
Control, because it's the web cam software itself creating the actual
preview display window, in the same location as your PictureBox control.

In other words, there's really nothing about your preview window that uses
anything in .NET or C# at all, never mind the PictureBox itself. Your
question seems better-directed at Logitech and/or a forum where questions
about using avicap32.dll are relevant. You are much more likely to get a
useful answer there. (Of course, it might just be "well, Logitech's own
software uses proprietary features not available through avicap32.dll",
but at least it'd be an actual answer :) ).

And by the way, when you do find a more appropriate forum, you may want to
be more specific about what you mean by "not as clear" and "highest
resolution". The former is especially vague, and the latter also seems
out of context.

Pete

On Friday, June 06, 2008 3:21 AM
djamila wrote:

WebCam Highest resolution in a picture Box
Hello,


I wrote a program in C# that displays a webcam capture in a picture
box. I used windows API for this task. The program works well but the
picture of the webcam capture in my program is not as clear as that of
the webcam viewer.

I need to have a very good resolution from my webcam. I bought the
webcam that I think it has the clearest view and the highest
resolution (Logitech QuickCam Pro 9000). I thought the first time that
the problem is in my webcam device, but after a long search I realized
that the problem is in my C# program..

Here is a summary of the program that I wrote. Please tell me which
part of the program has to be changed in order to have a better view
of my webcam.


public class WebCamCapture
{

[DllImport("user32.dll", EntryPoint = "SendMessageA")]
public static extern bool SendMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
CAPSTATUS lParam // second message parameter
);

[DllImport("user32.dll", EntryPoint = "SendMessageA")]
public static extern bool SendMessage(
int hWnd, // handle to destination window
int Msg, // message
bool wParam, // first message parameter
int lParam // second message parameter
);

[DllImport("user32.dll", EntryPoint = "SendMessageA")]
public static extern bool SendMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
int lParam // second message parameter
);

[DllImport("user32.dll")]
public static extern int SetWindowPos(
int hWnd,
int hWndInsertAfter,
int x,
int y,
int cx,
int cy,
int wFlags
);

[DllImport("user32.dll")]
public static extern bool SetWindowPos(int hndw);


public struct POINTAPI
{
int x;
int y;
}

[DllImport("user32.dll")]
public static extern bool DestroyWindow(int hWnd);

public struct CAPSTATUS
{
public int uiImageWidth; // Width of
the image
public int uiImageHeight; // Height of
the image
int fLiveWindow; // Now Previewing
video?
int fOverlayWindow; // Now Overlaying
video?
int fScale; // Scale image to
client?
POINTAPI ptScroll; // Scroll position
int fUsingDefaultPalette; // Using default driver
palette?
int fAudioHardware; // Audio hardware
present?
int fCapFileExists; // Does capture file
exist?
int dwCurrentVideoFrame; // # of video frames
cap'td
int dwCurrentVideoFramesDropped; // # of video frames
dropped
int dwCurrentWaveSamples; // # of wave samples
cap'td
int dwCurrentTimeElapsedMS; // Elapsed capture
duration
int hPalCurrent; // Current palette in
use
int fCapturingNow; // Capture in
progress?
int dwReturn; // Error value after any
operation
int wNumVideoAllocated; // Actual number of
video buffers
int wNumAudioAllocated; // Actual number of
audio buffers
}


[DllImport("avicap32.dll")]
public static extern int capCreateCaptureWindowA(
string lpszWindowName,
int dwStyle,
int x,
int y,
int nWidth,
short nHeight,
int hWndParent,
int nID
);

[DllImport("avicap32.dll")]
public static extern bool capGetDriverDescriptionA(
short wDriver,
string lpszName,
int cbName,
string lpszVer,
int cbVer
);

[DllImport("kernel32.Dll")]
public static extern int DeleteFile(string lpPathName);

}


And here the program that displays my webcam capture in a picture box.


bReturn = WebCamCapture.capGetDriverDescriptionA(x, strName, 100,
strVer, 100);
OpenPreviewWindow();
WebCamCapture.CAPSTATUS s = new WebCamCapture.CAPSTATUS();
bReturn = WebCamCapture.SendMessage(hHwnd, WM_CAP_GET_STATUS,
Marshal.SizeOf(s), s);
Debug.WriteLine(String.Format("Video Size {0} x {1}", s.uiImageWidth,
s.uiImageHeight));



private void OpenPreviewWindow()
{
int iHeight = this.pictureBox.Height;
int iWidth = this. pictureBox.Width;

// Open Preview window in picturebox
hHwnd = WebCamCapture.capCreateCaptureWindowA(iDevice.ToString(),
WS_VISIBLE | WS_CHILD, 0, 0, 1280, 1024, this.
pictureBox.Handle.ToInt32(), 0);

// Connect to device
if (WebCamCapture.SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice,
0))
{
// Set the preview scale
WebCamCapture.SendMessage(hHwnd, WM_CAP_SET_SCALE, true, 0);

//Set the preview rate in milliseconds
WebCamCapture.SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0);

//Start previewing the image from the camera
WebCamCapture.SendMessage(hHwnd, WM_CAP_SET_PREVIEW, true, 0);

// Resize window to fit in picturebox
WebCamCapture.SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, this.
pictureBox.Width, this. pictureBox.Height, SWP_NOMOVE | SWP_NOZORDER);
}
else
{
// Error connecting to device close window
hHwnd = 0;
}
}


Submitted via EggHeadCafe - Software Developer Portal of Choice
WPF Binding Beyond the Limitation of Name Scopes
http://www.eggheadcafe.com/tutorials/aspnet/ef583104-e507-491d-b05f-49faac8854c8/wpf-binding-beyond-the-li.aspx