简体   繁体   中英

How to find camera attached by USB cable, using C#, so I can then copy stored images

I want to attach a camera to a computer by USB cable, and copy images from the camera's SD card to the computer. I can't locate the images. I'd like to detect any camera, not just Canon... but I need to solve Canon first. (Removing the SD card from the camera is not a usable solution.)

When the camera is attached to the computer using a USB cable, the camera shows up under the "This PC" folder. But "This PC" is not a normal folder but a special one which doesn't show up under any normal path. Windows apparently uses "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" to get to it internally, but I can find no way to use that in C#. The environment variable which would point to it returns "", and is documented as such. I tried listing all the disk drives using DriveInfo.GetDrives(). But the camera does not show up... apparently it is only a device and not a drive (even though when you click on it you get a folder view).

The only references I can find to accessing Canon camera images all use a Canon SDK which only works with their EOS model cameras to provide full remote control.

EQUIPMENT: Using Visual Studio 2019, C#, Windows 10 Pro, and a Canon PowerShot SX700 HS camera

PURPOSE: I'm trying to make a program for an elderly friend so he can easily download photos to his computer. He knows how to attach his camera with the USB cable, but opening folders, copying, drag and drop, etc. confuses him. It has to be all automatic... attach camera and a C# program copies all the pictures and videos to the computer hard drive and then opens "windows explorer" to the destination folder on the hard drive. Accessing the SD card directly (removing it from the camera) is not an option, as my friend has physical limitations which prevent it.

Using this script and the nuget package "MediaDevices" from "Ralf Beckers" you can copy all files from your camera like this in the script below. Just make sure to change stuff like "DEVICE NAME", @"\Internal Storage\" and the location to my desktop:)":

using System.IO;
using MediaDevices;

private void button3_Click(object sender, EventArgs e)
        {
            var devices = MediaDevice.GetDevices();
            foreach(var device in devices)
            {
                MessageBox.Show(device.FriendlyName);
                if (device.FriendlyName == "DEVICE NAME")
                {
                    device.Connect();

                    var drs = device.GetFiles(@"\Internal Storage\");
                    foreach (string str in drs)
                    {
                        MessageBox.Show(str);
                        MemoryStream memoryStream = new System.IO.MemoryStream();
                        device.DownloadFile(str, memoryStream);
                        memoryStream.Position = 0;
                        WriteStreamToDisk(@"C:\Users\nikla\Desktop\temp\" + str.Substring(str.LastIndexOf("\\") + 1), memoryStream);
                    }

                }
            }

        }

        static void WriteStreamToDisk(string filePath, MemoryStream memoryStream)
        {
            using (FileStream file = new FileStream(filePath, FileMode.Create, System.IO.FileAccess.Write))
            {
                byte[] bytes = new byte[memoryStream.Length];
                memoryStream.Read(bytes, 0, (int)memoryStream.Length);
                file.Write(bytes, 0, bytes.Length);
                memoryStream.Close();
            }
        }

You can index more files with device.GetDirectories(@"\Internal Storage\") and run through them with a foreach loop.

Credit to "@ZackOfAllTrades" for bringing this nuget packet to my attention

When connecting a camera (or smartphone) to a computer, there are two outcomes:

  1. The Camera acts like a USB stick or other mass storage device. That is the ideal case.
  2. The camera does not act like a USB stick. Wich means you need a special programm from the manufacturer to access the pictures.

If it is a case 1, it really works like every USB stick would.

If it is a case 2, then the programm is the only way. You are tied to exactly what it offers.

Personally I would never buy a camera that is a case 2. While I can appreciate the idea of not wanting normal users to delete folders they should not interact with, the need for a special programm is really to much of a hassle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM