Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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

C#
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;
    }
}
Posted
Updated 2-Mar-11 22:12pm
v3
Comments
Sergey Alexandrovich Kryukov 2-Mar-11 18:05pm    
Excuse me, who will volunteer to so long and purely formatted code. Use pre lang="C#" tags, format code properly, but develop minimal code to demonstrate the problem, specially for the Question. Also, you need to separate UI and data acquisition, not just for Question, but for yourself.
--SA
Henry Minute 2-Mar-11 18:05pm    
Waaaay too much code to expect people to wade through.

Please try to cut it down to just the area where the error occurs and any methods called by that code.
Dave Kreskowiak 2-Mar-11 19:01pm    
Are you using the latest and greater Logic driver for you webcam?
NuttingCDEF 3-Mar-11 4:20am    
Code now updated to remove everything that isn't directly related to the WIA stuff (including removing all the try/catches etc.). But note that the problem is not that the code never works, but that their are fairly common driver exceptions.

Update Drivers usually works (so long as there have been no previous errors since the PC was last rebooted), Update Formats often works, TakePhoto sometimes works (and when it does will often work many times - 100s - without an error but can then still fall over). Errors that I've seen have been at Connect and ExecuteCommand.

To answer Dave Kreskowiak's query, yes, as far as I am aware the drivers are the latest. This was only installed a couple of months ago, all updates available from Logitech that I can find are installed (Logitech Updater in the Quickcam software says everything is up to date) and there are no outstanding updates available from Microsoft Update.

Many thanks, David

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