D
djamila
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;
}
}
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;
}
}