Question

I am using WIA to take a photo programatically and this image will then be copied over to a specific folder in my desktop. Everything is going well except for the saving part.

void deviceManager_OnEvent(string EventID, string DeviceID, string ItemID)
    {

        for (int i = 1; i <= d.Items.Count; i++)
        {
            wiaImageFile = (WIA.ImageFile)d.Items[i].Transfer(FormatID.wiaFormatJPEG);
            wiaImageFile.SaveFile(Properties.Settings.Default.FolderNameRaw + "\\1.jpg");
            if (wiaImageFile != null)
                Marshal.ReleaseComObject(wiaImageFile);
        }
    }

In this code, my d.Items.Count is constantly increasing(Eg. 29 which is totally diff from the actual count) even though I only have 2 photos in my camera's SD card. Is there a way to correctly transfer or even cut the file over to my computer? This is the exception that i receive. enter image description here

Was it helpful?

Solution

The answer to this is merely choosing the last entry in the index. For my case, I only need to grab the latest photo so this works well.

if (EventID == WIA.EventID.wiaEventDeviceConnected)
        {
            Console.WriteLine("Connected: D5100");
        }
        if (EventID == WIA.EventID.wiaEventItemCreated)
        {
            if (d != null)
            {
                foreach (Property p in d.Properties)
                {
                    if (p.Name.Equals("Pictures Taken"))
                        Console.WriteLine("Taken");
                }

                wiaImageFile = (WIA.ImageFile)(d.Items[d.Items.Count].Transfer(FormatID.wiaFormatJPEG));
                wiaImageFile.SaveFile(Properties.Settings.Default.FolderNameRaw + "\\" + imageCount + ".jpg");
                imageCount++;
            }
        }

This works very well. Except that if the file exists already for whatever reasons, you need to do a try catch and fix the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top