I'm trying to develop an application for Win 10 device which is going to run on factory-resetted device (using windows 10 build 15063). This app has to have access to hardware (so I cannot use UWP, because UWP runs in sandbox mode) and also has to have capability to take pictures using all cameras device has(using camera picker of some sort). My main problem is that I'm only allowed to use built-in .NET library to install and launch app so I cannot use any 3'rd party frameworks to simplify camera capture (for security reasons, also whole app has to be contained in single .exe file) and Windows.media.capture works only in UWP which is useless for me.
Can anyone know which default library I can use to create simple-enough app to have camera capturing capability?
You may use WIN32API
to achieve what you’re looking for but it’s a bit complicated.
Have a look at capCreateCaptureWindowW
from the library avicap32.dll
.
Try to adapt this code to suit your needs (it may have some bugs, but it's a good start.)
public class Camera
{
[DllImport("kernel32.dll")]
private static extern void Sleep(int dwMilliseconds);
[DllImport("kernel32.dll")]
private static extern int Beep(int dwFreq, int dwDuration);
[DllImport("avicap32.dll", EntryPoint = "capCreateCaptureWindowW")]
private static extern int capCreateCaptureWindow(string lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, int hWndParent
private const int WS_VISIBLE = 0x10000000;
private const int WS_CHILD = 0x40000000;
[DllImport("user32.dll", EntryPoint = "SendMessageW")]
private static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "SendMessageW")]
private static extern int SendMessageFL(int hwnd, int wMsg, int wParam, string lParam);
[DllImport("user32.dll", EntryPoint = "SendMessageW")]
private static extern int SendMessageSD(int hwnd, int wMsg, string wParam, int lParam);
private const int WM_USER = 0x400;
private const int WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10);
private const int WM_CAP_START = WM_USER;
private const int WM_CAP_FILE_SAVEDIBA = (WM_CAP_START + 25);
private const int WM_CAP_SET_SCALE = WM_USER + 53;
private const int WM_CAP_SET_PREVIEW = WM_USER + 50;
private const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52;
private const int WM_CAP_FILE_SAVEDIB = WM_USER + 25;
private const int WM_CAP_DRIVER_DISCONNECT = WM_USER + 11;
private string _camtitle; // a pointer to HDCAM
private int hWebcam;
private const int nDevice = 0;
private const int nFPS = 50;
private string _filename; // image.bmp filename
public int getCAM(string cam_title, int cam_x, int cam_y, int cam_width, int cam_height, IntPtr HWNDparent, int cam_ID)
{
_camtitle = cam_title;
hWebcam = capCreateCaptureWindow(cam_title, WS_VISIBLE + WS_CHILD, cam_x, cam_y, cam_width, cam_height, HWNDparent.ToInt32(), cam_ID);
return hWebcam;
}
public string NewFileNAME()
{
DateTime DT = new DateTime();
DT.Date.ToString();
return "file-" + DT.Date.ToString();
}
public void startCAM()
{
SendMessage(hWebcam, WM_CAP_DRIVER_CONNECT, nDevice, 0);
SendMessage(hWebcam, WM_CAP_SET_SCALE, 1, 0);
SendMessage(hWebcam, WM_CAP_SET_PREVIEWRATE, nFPS, 0);
SendMessage(hWebcam, WM_CAP_SET_PREVIEW, 1, 0);
}
public void captureCAM(string BMPfilename)
{
_filename = BMPfilename;
SendMessageFL(hWebcam, WM_CAP_FILE_SAVEDIBA, 0, _filename);
}
public void stopCAM()
{
SendMessageSD(hWebcam, WM_CAP_DRIVER_DISCONNECT, _camtitle, 0);
}
}
Or you can use the Windows 10 API
and calling them from a Windows Form/WPF application. I believe this reference will help you
Update (Helpful references)
Webcamera, Multithreading and VFW
Code Snippets – Capture WebCam Image in C#