Hello.
I faced the task of receiving photos from a camera in my program on c #, and it was necessary so that the user pressed a button in the program, or some kind of program event happened, and we received a picture from the camera into the program and then somehow processed / saved / sent, in general, did something with it in the same program way.
As it turned out, the manufacturers of digital SLR cameras have a special SDK through which you can programmatically access this camera and control it. I have a Nikon D5200 camera, although for Sony and Canon, it seems, I also saw a similar SDK.
')
The purpose of the article is to tell about the opportunity and show a short example. As you know, if you know that something can be done, then learn how to do it - a couple of trivia.
So, what do we need:
First, you need to download the SDK itself from the Nikon site:
sdk.nikonimaging.com/applyYou will have to register and become an honorary developer of Nikon, and when registering you need to use the organization’s mailbox, there are no public mailboxes. There you can see a list of supported cameras, unfortunately cheap soap boxes are not supported.
Secondly, for simplicity, I recommend using the one already written by NikonCSWrapper:
sourceforge.net/p/nikoncswrapper/wiki/HomeNext, create your project (in Visual Studio), connect the link to the Nikon wrapper, and note: copy files to the binary directory: NkdPTP.dll and Type0009.md3 (here the numbers may differ depending on the camera), which can be found in downloaded SDK.
Now a small example of how to photograph:
First, we define the device manager:
NikonManager manager = new NikonManager("Type0009.md3");
Then we attach a device add event handler to the manager:
manager.DeviceAdded += manager_DeviceAdded;
In the handler code, I have the following text:
void manager_DeviceAdded(NikonManager sender, NikonDevice device)
{
_device = device;
this.Text = _device.Name;
_device.ImageReady += _device_ImageReady;
}
Here I save the link to the new device and link the handler to the ImageReady event, which will be triggered when the camera takes pictures, the event is triggered, and when you command to take a picture from the program, and when you press the button on the camera itself.
The event handler for my photography looks like this:
void _device_ImageReady(NikonDevice sender, NikonImage image)
{
MemoryStream ms = new MemoryStream(image.Buffer);
Image img = Image.FromStream(ms);
ms.Close();
pictureBox1.Image = img;
}
Here I simply get access to the buffer in which the photo sits and through the MemoryStream I load it into Image, which I then send in the pictureBox to display it on the form. As you know, with Image you can do anything at all, my code is just for example.
To take a camera from the program code, you need to call the Capture () method:
_device.Capture();
Well, when you exit the program, you must close the device manager, otherwise, then do not connect to it until you restart the computer:
manager.Shutdown();
In general, through the SDK, you can do a lot of things with your camera: get and set any (?) Camera settings, take photos, shoot videos, focus automatically and manually (from the program), get live video from the viewfinder.
I have everything on this, I hope someone will find this information useful, Thank you for your attention.