Take a picture automatically in C# using WIA

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
Hi everyone,
Im using WIALib to access my webcam. The code Im 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 isnt yet fully automated. The only way I found to retrieve the pictures taken by the webcam, is using this:
<div style="background-color:white; color:black
<pre>wiaPics = wiaRoot.GetItemsFromUI( WiaFlag.SingleImage, WiaIntent.ImageTypeColor ) <span style="color:blue as CollectionClass;

[/code]

But this asks the user to select the picture. And I always want the last picture taken. So Im trying this way:
<div style="background-color:white; color:black
<pre><span style="color:blue string imageFileName = Path.GetTempFileName(); <span style="color:green // create temporary file for image

wiaItem = wiaRoot.TakePicture(); <span style="color:green // take a picture

Cursor.Current = Cursors.WaitCursor; <span style="color:green // could take some time

<span style="color:blue this.Refresh();

wiaItem.Transfer(imageFileName, <span style="color:blue false); <span style="color:green // transfer picture to our temporary file

pictureBox1.Image = Image.FromFile(imageFileName); <span style="color:green // create Image instance from file

Marshal.ReleaseComObject(wiaItem);

[/code]

But the method TakePicture() returns null, and so I cant 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 dont get it why it doesnt return a value.
To summarize, I either need one of this two:
<ol>
Get TakePicture() to work, returning a value I can use. Access the list of the webcams pictures automatically, so I can retrieve the last picture taken.
</ol>
Best regards and thanks for the help, Micael.

View the full article
 
Back
Top