Take a picture automatically using a webcam in C # using WIA

I use WIALib to access my webcam. The code I'm developing is pretty simple: when you click the button, the webcam is captured and then displayed in the image window.

I can already take pictures using my webcam, but it is not yet fully automated. The only way to find pictures taken with a webcam is as follows:

wiaPics = wiaRoot.GetItemsFromUI( WiaFlag.SingleImage, WiaIntent.ImageTypeColor ) as CollectionClass; 

But this asks the user to select an image. And I always need the last photo. Therefore, I try:

 string imageFileName = Path.GetTempFileName(); // create temporary file for image wiaItem = wiaRoot.TakePicture(); // take a picture Cursor.Current = Cursors.WaitCursor; // could take some time this.Refresh(); wiaItem.Transfer(imageFileName, false); // transfer picture to our temporary file pictureBox1.Image = Image.FromFile(imageFileName); // create Image instance from file Marshal.ReleaseComObject(wiaItem); 

But the TakePicture () method returns null, so I can not pass the image. The strangest thing is that the picture was really made after the TakePicture () method was called, because if I manually move around the webcam, there will be an image! I just don't understand why it is not returning a value.

To summarize, I need either one of two things: 1. Get TakePicture () to work, returning a value that I can use. 2. The list of webcam photos is accessed automatically, so I can get the last image taken.

Best regards and thanks for the help, Michael.

+4
source share
1 answer

From what I see, wiaItem = wiaRoot.TakePicture() is going the wrong way. Try the following:

 string imageFileName; wiaRoot.TakePicture( out takenFileName); pictureBox1.Image = Image.FromFile(imageFileName); 

TakePicture saves the image to a file and returns the new file name as the output parameter.

Edit for your comment - are you using Windows 7 version of WiaLib version? If so, try something like this:

 var manager = new DeviceManagerClass(); Item wiaItem; Device device = null; foreach (var info in manager.DeviceInfos) { if (info.DeviceID == DESIRED_DEVICE_ID) { device = info.Connect(); wiaItem = device.ExecuteCommand(CommandID.wiaCommandTakePicture); } } 

where you use ExecuteCommand with the well-known guid (also open from the shell of COM interop), and not TakePicture. In any case, it worked on my webcam.

+3
source

All Articles