4

I'm using WIALib to access my webcam. The code I'm developing is pretty simple: when a button is pressed a webcam picture is taken, and then displayed in a picture box.

I can already take pictures with my webcam, but it isn't yet fully automated. The only way I found to retrieve the pictures taken by the webcam, is using this:

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

But this asks the user to select the picture. And I always want the last picture taken. So I'm trying this way:

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 method TakePicture() returns null, and so I can't transfer the image. The strangest thing is that the picture was really taken after the method TakePicture() was called, since if I go to the webcam manually the picture is there! I just don't get it why it doesn't return a value.

To summarize, I either need one of this two: 1. Get TakePicture() to work, returning a value I can use. 2. Access the list of the webcam's pictures automatically, so I can retrieve the last picture taken.

Best regards and thanks for the help, Micael.

Micael
  • 53
  • 1
  • 1
  • 5

1 Answers1

3

From what I can see, wiaItem = wiaRoot.TakePicture() is going down the wrong path. Try this:

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

TakePicture saves a picture to a file, and returns the new file's name as an output parameter.

Edit per your comment - are you using the "windows 7 version" of WiaLib? 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 the ExecuteCommand with the well known guid (also exposed from the COM interop wrapper) rather than TakePicture. It worked for my webcam, in any case.

Philip Rieck
  • 32,368
  • 11
  • 87
  • 99
  • Thanks for the help Philip. The thing is, I'm getting the "No overload for method "TakePicture" takes 1 argument" error. :( – Micael Dec 01 '10 at 11:04
  • @Micael - if you're on the newer, see if my edit will work for you. – Philip Rieck Dec 01 '10 at 13:51
  • I don't know the version of my WiaLib. I'm working under WindowsXP Pro and Visual Studio 2010. When I pasted your code, the class DeviceManagerClass and Device weren't recognized. Do I need to add another reference, other than WiaLib? Thanks again :) – Micael Dec 01 '10 at 14:20
  • @Micael Make sure you are adding a COM reference to "Windows Image Acquisition Libarary 2.0". `Device` should be in the WIA namespace - make sure you have `using WIA`. You should then be able to find `WIA.DeviceManagerClass`. – Philip Rieck Dec 01 '10 at 14:38
  • Well, I guess I'm not using the windows 7 version of WIA, since I only have "Microsoft Windows Image Acquisition 1.01 Type Library". – Micael Dec 01 '10 at 15:21
  • @Micael well, I'm not familiar with that one at all. You can run wia2.0 on XP machines though: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=a332a77a-01b8-4de6-91c2-b7ea32537e29. – Philip Rieck Dec 01 '10 at 16:21
  • Thanks for all the help Philip! I tried your piece of code, and I'm having this exception [Exception from HRESULT: 0x802100839] on the line where the picture is supposed to be taken. – Micael Dec 02 '10 at 10:56
  • Do you have any idea what this exception mean? I've been googling around and found nothing :( – Micael Dec 03 '10 at 14:29
  • @Micael Sorry, I'm not familiar with that one at all. Are you certain that you have the right device? – Philip Rieck Dec 06 '10 at 00:29
  • Yes, I'm pretty sure the device is right... Anyway, thanks for all the help Philip, but I think I'll switch to DirectShow :) regards – Micael Dec 08 '10 at 13:37