Click here to Skip to main content
15,867,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to set Webcam on image control in wpf application.
My code is
C#
public void Attach(System.Windows.Controls.Image control)
       {
               //deviceHandle = Class1.Avicap32.capCreateCaptureWindow("", Class1.Constants.WS_VISIBLE | Class1.Constants.WS_CHILD, 0, 0, control.Width, control.Height, control.Handle, 0);//Here it works but in window form application
                 deviceHandle = Class1.Avicap32.capCreateCaptureWindow(string.Empty, Class1.Constants.WS_VISIBLE | Class1.Constants.WS_CHILD, 110, 224,0,0, new WindowInteropHelper(this).Handle, 0);//it works on my window but not at image control
               if (Class1.User32.SendMessage(deviceHandle, Class1.Constants.WM_CAP_DRIVER_CONNECT, (IntPtr)0, (IntPtr)0).ToInt32() > 0)
               {
                   Class1.User32.SendMessage(deviceHandle, Class1.Constants.WM_CAP_SET_SCALE, (IntPtr)(-1), (IntPtr)0);
                   Class1.User32.SendMessage(deviceHandle, Class1.Constants.WM_CAP_SET_PREVIEWRATE, (IntPtr)0x42, (IntPtr)0);
                   Class1.User32.SendMessage(deviceHandle, Class1.Constants.WM_CAP_SET_PREVIEW, (IntPtr)(-1), (IntPtr)0);
                   Class1.User32.SetWindowPos(deviceHandle, new IntPtr(0), 0, 0, 206,187, 2);

               }
               else
               {
                   MessageBox.Show("Webcam not found ");
                   Class1.User32.DestroyWindow(deviceHandle);
               }
       }
Posted
Updated 13-Mar-13 19:11pm
v2

1 solution

Use window.form control in wpf
in window tag
HTML
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

add a window.form.picturebox
HTML
<windowsformshost>
                                <wf:picturebox x:name="LiveImage" xmlns:x="#unknown" xmlns:wf="#unknown" />
                            </windowsformshost>

and now call attach method
C#
public void Attach(System.Windows.Controls.Image control)
        {
                deviceHandle = Class1.Avicap32.capCreateCaptureWindow("", Class1.Constants.WS_VISIBLE | Class1.Constants.WS_CHILD, 0, 0, control.Width, control.Height, control.Handle, 0);
                if (Class1.User32.SendMessage(deviceHandle, Class1.Constants.WM_CAP_DRIVER_CONNECT, (IntPtr)0, (IntPtr)0).ToInt32() > 0)
                {
                    Class1.User32.SendMessage(deviceHandle, Class1.Constants.WM_CAP_SET_SCALE, (IntPtr)(-1), (IntPtr)0);
                    Class1.User32.SendMessage(deviceHandle, Class1.Constants.WM_CAP_SET_PREVIEWRATE, (IntPtr)0x42, (IntPtr)0);
                    Class1.User32.SendMessage(deviceHandle, Class1.Constants.WM_CAP_SET_PREVIEW, (IntPtr)(-1), (IntPtr)0);
                    Class1.User32.SetWindowPos(deviceHandle, new IntPtr(0), 0, 0, 206,187, 2);
                  
                }
                else
                {
                    MessageBox.Show("Webcam not found ");
                    Class1.User32.DestroyWindow(deviceHandle);
                }   
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900