NOTE : I've updated the code to cut out everything that isn't directly related to the WIA stuff - hopefully this helps.
I'm exploring using Windows Image Acquistion from C# and have hit a problem. I have code that sometimes works fine but sometimes (often) throws a WIA_ERROR_EXCEPTION_IN_DRIVER exception (0x80210083), often at DeviceInfo.Connect or Device.ExecuteCommand(CommandID.wiaCommandTakePicture).
When it happens WIA seems to fall over completely - stopping my code and restarting it usually fails to find any WIA devices; WIA service usually cannot be stopped / restarted (hangs at 'Stopping') necessitating a reboot.
I'm using the Microsoft Windows Image Acquisition Library v2.0 (not the Microsoft Windows Image Acquisition 1.01 Type Library).
The camera I'm using is a Logitech Webcam 250.
Windows XP SP3, fully updated with all MS updates; Visual Studio 2010; targeting .NET Framework 4 Client Profile.
Relevant code below (I've included the whole file - search for Connect or ExecuteCommand to find the points where the problem appears).
Any assistance with what's causing this and how to prevent it would be much appreciated.
David
using WIA;
public partial class MainForm : Form
{
private void UpdateDevices()
{
DeviceManager ADeviceManager = new DeviceManager();
if (ADeviceManager != null)
{
DeviceInfos ADeviceInfos = ADeviceManager.DeviceInfos;
if (ADeviceInfos != null)
{
foreach (DeviceInfo ADeviceInfo in ADeviceInfos)
{
if ((ADeviceInfo.Type == WiaDeviceType.CameraDeviceType) || (ADeviceInfo.Type == WiaDeviceType.VideoDeviceType))
{
WIA.Properties AProperties = ADeviceInfo.Properties;
if (AProperties != null)
{
foreach (Property AProperty in AProperties)
{
if (((WiaPropertyType)(AProperty.Type)) == WiaPropertyType.StringPropertyType)
{
if (AProperty.Name == "Name")
{
DeviceBox.Items.Add(AProperty.get_Value().ToString());
}
}
Marshal.ReleaseComObject(AProperty);
}
Marshal.ReleaseComObject(AProperties);
}
}
Marshal.ReleaseComObject(ADeviceInfo);
}
Marshal.ReleaseComObject(ADeviceInfos);
}
Marshal.ReleaseComObject(ADeviceManager);
}
}
private void UpdateFormats()
{
DeviceInfo SelectedDeviceInfo = GetSelectedDeviceInfo();
if (SelectedDeviceInfo != null)
{
Device ADevice = SelectedDeviceInfo.Connect();
if (ADevice != null)
{
Thread.Sleep(500);
Item AnItem = ADevice.ExecuteCommand(CommandID.wiaCommandTakePicture);
if (AnItem != null)
{
Formats AFormats = AnItem.Formats;
if (AFormats != null)
{
foreach (string AFormatID in AFormats)
{
string AFormat = GetFormatString(AFormatID);
FormatBox.Items.Add(AFormat);
if (AFormat == SelectedFormat)
{
Found = true;
}
}
Marshal.ReleaseComObject(AFormats);
}
Marshal.ReleaseComObject(AnItem);
}
Marshal.ReleaseComObject(ADevice);
}
Marshal.ReleaseComObject(SelectedDeviceInfo);
}
}
private void TakePhoto()
{
DeviceInfo SelectedDeviceInfo = GetSelectedDeviceInfo();
if (SelectedDeviceInfo != null)
{
Device TheDevice = SelectedDeviceInfo.Connect();
if (TheDevice != null)
{
Item AnItem = TheDevice.ExecuteCommand(CommandID.wiaCommandTakePicture);
if (AnItem != null)
{
string AFileName = Path.Combine(SaveInFolder, DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss.fff") + " - " + (PhotoNum++).ToString("000000") + "." + SelectedFormat);
ImageFile AnImageFile = (ImageFile)(AnItem.Transfer(GetFormat(SelectedFormat)));
AnImageFile.SaveFile(AFileName);
UpdatePicture(AFileName);
Marshal.ReleaseComObject(AnItem);
}
Marshal.ReleaseComObject(TheDevice);
}
Marshal.ReleaseComObject(SelectedDeviceInfo);
}
}
private DeviceInfo GetSelectedDeviceInfo()
{
DeviceInfo Result = null;
DeviceManager ADeviceManager = new DeviceManager();
if (ADeviceManager != null)
{
DeviceInfos ADeviceInfos = ADeviceManager.DeviceInfos;
if (ADeviceInfos != null)
{
foreach (DeviceInfo ADeviceInfo in ADeviceInfos)
{
if ((ADeviceInfo.Type == WiaDeviceType.CameraDeviceType) || (ADeviceInfo.Type == WiaDeviceType.VideoDeviceType))
{
WIA.Properties AProperties = ADeviceInfo.Properties;
if (AProperties != null)
{
foreach (Property AProperty in AProperties)
{
if (AProperty.Name == "Name")
{
if (AProperty.get_Value().ToString() == SelectedDeviceName)
{
Result = ADeviceInfo;
break;
}
}
Marshal.ReleaseComObject(AProperty);
}
Marshal.ReleaseComObject(AProperties);
}
}
if (Result != ADeviceInfo)
{
Marshal.ReleaseComObject(ADeviceInfo);
}
}
Marshal.ReleaseComObject(ADeviceInfos);
}
Marshal.ReleaseComObject(ADeviceManager);
}
return Result;
}
}