Untitled diff

Created Diff never expires
using System;
using System;
using System.IO;
using System.IO;
using System.Linq;
using System.Linq;
using System.Drawing;
using System.Drawing;
using System.Threading;
using System.Threading;
using System.Globalization;
using System.Globalization;
using System.Drawing.Imaging;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;


namespace EDSDKLib
namespace EDSDKLib
{
{
/// <summary>
/// <summary>
/// Handles the Canon SDK
/// Handles the Canon SDK
/// </summary>
/// </summary>
public class SDKHandler : IDisposable
public class SDKHandler : IDisposable
{
{
#region Variables
#region Left Variables


/// <summary>
/// <summary>
/// The used camera
/// The used camera
/// </summary>
/// </summary>
public Camera MainCamera { get; private set; }
public Camera LeftCamera { get; private set; }
/// <summary>
/// <summary>
/// States if a session with the MainCamera is opened
/// States if a session with the MainCamera is opened
/// </summary>
/// </summary>
public bool CameraSessionOpen { get; private set; }
public bool LeftCameraSessionOpen { get; private set; }
/// <summary>
/// <summary>
/// States if the live view is on or not
/// States if the live view is on or not
/// </summary>
/// </summary>
public bool IsLiveViewOn { get; private set; }
public bool IsLeftLiveViewOn { get; private set; }
/// <summary>
/// <summary>
/// States if camera is recording or not
/// States if camera is recording or not
/// </summary>
/// </summary>
public bool IsFilming { get; private set; }
public bool IsLeftFilming { get; private set; }
/// <summary>
/// <summary>
/// Directory to where photos will be saved
/// Directory to where photos will be saved
/// </summary>
/// </summary>
public string ImageSaveDirectory { get; set; }
public string LeftImageSaveDirectory { get; set; }
/// <summary>
/// <summary>
/// The focus and zoom border rectangle for live view (set after first use of live view)
/// The focus and zoom border rectangle for live view (set after first use of live view)
/// </summary>
/// </summary>
public EDSDK.EdsRect Evf_ZoomRect { get; private set; }
public EDSDK.EdsRect Left_Evf_ZoomRect { get; private set; }
/// <summary>
/// <summary>
/// The focus and zoom border position of the live view (set after first use of live view)
/// The focus and zoom border position of the live view (set after first use of live view)
/// </summary>
/// </summary>
public EDSDK.EdsPoint Evf_ZoomPosition { get; private set; }
public EDSDK.EdsPoint Left_Evf_ZoomPosition { get; private set; }
/// <summary>
/// <summary>
/// The cropping position of the enlarged live view image (set after first use of live view)
/// The cropping position of the enlarged live view image (set after first use of live view)
/// </summary>
/// </summary>
public EDSDK.EdsPoint Evf_ImagePosition { get; private set; }
public EDSDK.EdsPoint Left_Evf_ImagePosition { get; private set; }
/// <summary>
/// <summary>
/// The live view coordinate system (set after first use of live view)
/// The live view coordinate system (set after first use of live view)
/// </summary>
/// </summary>
public EDSDK.EdsSize Evf_CoordinateSystem { get; private set; }
public EDSDK.EdsSize Left_Evf_CoordinateSystem { get; private set; }
/// <summary>
/// <summary>
/// States if the Evf_CoordinateSystem is already set
/// States if the Evf_CoordinateSystem is already set
/// </summary>
/// </summary>
public bool IsCoordSystemSet = false;
public bool IsLeftCoordSystemSet = false;
/// <summary>
/// <summary>
/// Handles errors that happen with the SDK
/// Handles errors that happen with the SDK
/// </summary>
/// </summary>
public uint Error
public uint LeftError
{
{
get { return EDSDK.EDS_ERR_OK; }
get { return EDSDK.EDS_ERR_OK; }
set { if (value != EDSDK.EDS_ERR_OK) throw new Exception("SDK Error: 0x" + value.ToString("X")); }
set { if (value != EDSDK.EDS_ERR_OK) throw new Exception("SDK Error: 0x" + value.ToString("X")); }
}
}




/// <summary>
/// <summary>
/// States if a finished video should be downloaded from the camera
/// States if a finished video should be downloaded from the camera
/// </summary>
/// </summary>
private bool DownloadVideo;
private bool Left_DownloadVideo;
/// <summary>
/// <summary>
/// For video recording, SaveTo has to be set to Camera. This is to store the previous setting until after the filming.
/// For video recording, SaveTo has to be set to Camera. This is to store the previous setting until after the filming.
/// </summary>
/// </summary>
private uint PrevSaveTo;
private uint Left_PrevSaveTo;
/// <summary>
/// <summary>
/// The thread on which the live view images will get downloaded continuously
/// The thread on which the live view images will get downloaded continuously
/// </summary>
/// </summary>
private Thread LVThread;
private Thread Left_LVThread;
/// <summary>
/// <summary>
/// If true, the live view will be shut off completely. If false, live view will go back to the camera.
/// If true, the live view will be shut off completely. If false, live view will go back to the camera.
/// </summary>
/// </summary>
private bool LVoff;
private bool Left_LVoff;


#endregion
#endregion


#region Right Variables

/// <summary>
/// The used camera
/// </summary>
public Camera RightCamera { get; private set; }
/// <summary>
/// States if a session with the MainCamera is opened
/// </summary>
public bool RightCameraSessionOpen { get; private set; }
/// <summary>
/// States if the live view is on or not
/// </summary>
public bool IsRightLiveViewOn { get; private set; }
/// <summary>
/// States if camera is recording or not
/// </summary>
public bool IsRightFilming { get; private set; }
/// <summary>
/// Directory to where photos will be saved
/// </summary>
public string RightImageSaveDirectory { get; set; }
/// <summary>
/// The focus and zoom border rectangle for live view (set after first use of live view)
/// </summary>
public EDSDK.EdsRect Right_Evf_ZoomRect { get; private set; }
/// <summary>
/// The focus and zoom border position of the live view (set after first use of live view)
/// </summary>
public EDSDK.EdsPoint Right_Evf_ZoomPosition { get; private set; }
/// <summary>
/// The cropping position of the enlarged live view image (set after first use of live view)
/// </summary>
public EDSDK.EdsPoint Right_Evf_ImagePosition { get; private set; }
/// <summary>
/// The live view coordinate system (set after first use of live view)
/// </summary>
public EDSDK.EdsSize Right_Evf_CoordinateSystem { get; private set; }
/// <summary>
/// States if the Evf_CoordinateSystem is already set
/// </summary>
public bool IsRightCoordSystemSet = false;
/// <summary>
/// Handles errors that happen with the SDK
/// </summary>
public uint RightError
{
get { return EDSDK.EDS_ERR_OK; }
set { if (value != EDSDK.EDS_ERR_OK) throw new Exception("SDK Error: 0x" + value.ToString("X")); }
}


/// <summary>
/// States if a finished video should be downloaded from the camera
/// </summary>
private bool Right_DownloadVideo;
/// <summary>
/// For video recording, SaveTo has to be set to Camera. This is to store the previous setting until after the filming.
/// </summary>
private uint Right_PrevSaveTo;
/// <summary>
/// The thread on which the live view images will get downloaded continuously
/// </summary>
private Thread Right_LVThread;
/// <summary>
/// If true, the live view will be shut off completely. If false, live view will go back to the camera.
/// </summary>
private bool Right_LVoff;

#endregion
#region Events
#region Events


#region SDK Events
#region SDK Events


public event EDSDK.EdsCameraAddedHandler SDKCameraAddedEvent;
public event EDSDK.EdsCameraAddedHandler SDKCameraAddedEvent;
public event EDSDK.EdsObjectEventHandler SDKObjectEvent;
public event EDSDK.EdsObjectEventHandler SDKObjectEvent;
public event EDSDK.EdsProgressCallback SDKProgressCallbackEvent;
public event EDSDK.EdsProgressCallback SDKProgressCallbackEvent;
public event EDSDK.EdsPropertyEventHandler SDKPropertyEvent;
public event EDSDK.EdsPropertyEventHandler SDKPropertyEvent;
public event EDSDK.EdsStateEventHandler SDKStateEvent;
public event EDSDK.EdsStateEventHandler SDKStateEvent;


#endregion
#endregion


#region Custom Events
#region Custom Events


public delegate void CameraAddedHandler();
public delegate void CameraAddedHandler();
public delegate void ProgressHandler(int Progress);
public delegate void ProgressHandler(int Progress);
public delegate void StreamUpdate(Stream img);
public delegate void StreamUpdate(Stream img);
public delegate void BitmapUpdate(Bitmap bmp);
public delegate void BitmapUpdate(Bitmap bmp);


/// <summary>
/// <summary>
/// Fires if a camera is added
/// Fires if a camera is added
/// </summary>
/// </summary>
public event CameraAddedHandler CameraAdded;
public event CameraAddedHandler CameraAdded;
/// <summary>
/// <summary>
/// Fires if any process reports progress
/// Fires if any process reports progress
/// </summary>
/// </summary>
public event ProgressHandler ProgressChanged;
public event ProgressHandler ProgressChanged;
/// <summary>
/// <summary>
/// Fires if the live view image has been updated
/// Fires if the live view image has been updated
/// </summary>
/// </summary>
public event StreamUpdate LiveViewUpdated;
public event StreamUpdate LiveViewUpdated;
/// <summary>
/// <summary>
/// If the camera is disconnected or shuts down, this event is fired
/// If the camera is disconnected or shuts down, this event is fired
/// </summary>
/// </summary>
public event EventHandler CameraHasShutdown;
public event EventHandler CameraHasShutdown;
/// <summary>
/// <summary>
/// If an image is downloaded, this event fires with the downloaded image.
/// If an image is downloaded, this event fires with the downloaded image.
/// </summary>
/// </summary>
public event BitmapUpdate ImageDownloaded;
public event BitmapUpdate ImageDownloaded;


#endregion
#endregion


#endregion
#endregion


#region Basic SDK and Session handling
#region Basic SDK and Session handling


/// <summary>
/// <summary>
/// Initializes the SDK and adds events
/// Initializes the SDK and adds events
/// </summary>
/// </summary>
public SDKHandler()
public SDKHandler()
{
{
//initialize SDK
//initialize SDK
Error = EDSDK.EdsInitializeSDK();
LeftError = EDSDK.EdsInitializeSDK();
STAThread.Init();
STAThread.Init();
//subscribe to camera added event (the C# event and the SDK event)
//subscribe to camera added event (the C# event and the SDK event)
SDKCameraAddedEvent += new EDSDK.EdsCameraAddedHandler(SDKHandler_CameraAddedEvent);
SDKCameraAddedEvent += new EDSDK.EdsCameraAddedHandler(SDKHandler_CameraAddedEvent);
EDSDK.EdsSetCameraAddedHandler(SDKCameraAddedEvent, IntPtr.Zero);
EDSDK.EdsSetCameraAddedHandler(SDKCameraAddedEvent, IntPtr.Zero);


//subscribe to the camera events (for the C# events)
//subscribe to the camera events (for the C# events)
SDKStateEvent += new EDSDK.EdsStateEventHandler(Camera_SDKStateEvent);
SDKStateEvent += new EDSDK.EdsStateEventHandler(Camera_SDKStateEvent);
SDKPropertyEvent += new EDSDK.EdsPropertyEventHandler(Camera_SDKPropertyEvent);
SDKPropertyEvent += new EDSDK.EdsPropertyEventHandler(Camera_SDKPropertyEvent);
SDKProgressCallbackEvent += new EDSDK.EdsProgressCallback(Camera_SDKProgressCallbackEvent);
SDKProgressCallbackEvent += new EDSDK.EdsProgressCallback(Camera_SDKProgressCallbackEvent);
SDKObjectEvent += new EDSDK.EdsObjectEventHandler(Camera_SDKObjectEvent);
SDKObjectEvent += new EDSDK.EdsObjectEventHandler(Camera_SDKObjectEvent);
}
}
/// <summary>
/// <summary>
/// Get a list of all connected cameras
/// Get a list of all connected cameras
/// </summary>
/// </summary>
/// <returns>The camera list</returns>
/// <returns>The camera list</returns>
public List<Camera> GetCameraList()
public List<Camera> GetCameraList()
{
{
IntPtr camlist;
IntPtr camlist;
//get list of cameras
//get list of cameras
Error = EDSDK.EdsGetCameraList(out camlist);
LeftError = EDSDK.EdsGetCameraList(out camlist);


//get each camera from camlist
//get each camera from camlist
int c;
int c;
//get amount of connected cameras
//get amount of connected cameras
Error = EDSDK.EdsGetChildCount(camlist, out c);
LeftError = EDSDK.EdsGetChildCount(camlist, out c);
List<Camera> OutCamList = new List<Camera>();
List<Camera> OutCamList = new List<Camera>();
for (int i = 0; i < c; i++)
for (int i = 0; i < c; i++)
{
{
IntPtr cptr;
IntPtr cptr;
//get pointer to camera at index i
//get pointer to camera at index i
Error = EDSDK.EdsGetChildAtIndex(camlist, i, out cptr);
LeftError = EDSDK.EdsGetChildAtIndex(camlist, i, out cptr);
OutCamList.Add(new Camera(cptr));
OutCamList.Add(new Camera(cptr));
}
}
return OutCamList;
return OutCamList;
}
}


/// <summary>
/// <summary>
/// Opens a session with given camera
/// Opens a session with given camera
/// </summary>
/// </summary>
/// <param name="newCamera">The camera which will be used</param>
/// <param name="newCamera">The camera which will be used</param>
public void OpenSession(Camera newCamera)
/// disabled for the moment
private void OpenSession(Camera newCamera)
{
if (LeftCamera == newCamera)
{
if (LeftCameraSessionOpen) CloseLeftSession();
if (newCamera != null)
{
//open a session
SendSDKCommand(delegate { LeftError = EDSDK.EdsOpenSession(LeftCamera.Ref); });
//subscribe to the camera events (for the SDK)
EDSDK.EdsSetCameraStateEventHandler(LeftCamera.Ref, EDSDK.StateEvent_All, SDKStateEvent, LeftCamera.Ref);
EDSDK.EdsSetObjectEventHandler(LeftCamera.Ref, EDSDK.ObjectEvent_All, SDKObjectEvent, LeftCamera.Ref);
EDSDK.EdsSetPropertyEventHandler(LeftCamera.Ref, EDSDK.PropertyEvent_All, SDKPropertyEvent, LeftCamera.Ref);
LeftCameraSessionOpen = true;
}
}
else if (RightCamera == newCamera)
{
if (RightCameraSessionOpen) CloseRightSession();
if (newCamera != null)
{
//open a session
SendSDKCommand(delegate { RightError = EDSDK.EdsOpenSession(RightCamera.Ref); });
//subscribe to the camera events (for the SDK)
EDSDK.EdsSetCameraStateEventHandler(RightCamera.Ref, EDSDK.StateEvent_All, SDKStateEvent, RightCamera.Ref);
EDSDK.EdsSetObjectEventHandler(RightCamera.Ref, EDSDK.ObjectEvent_All, SDKObjectEvent, RightCamera.Ref);
EDSDK.EdsSetPropertyEventHandler(RightCamera.Ref, EDSDK.PropertyEvent_All, SDKPropertyEvent, RightCamera.Ref);
RightCameraSessionOpen = true;
}
}
}
public void OpenSession(Camera leftCamera, Camera rightCamera)
{
{
if (CameraSessionOpen) CloseSession();
if (LeftCameraSessionOpen) CloseLeftSession();
if (newCamera != null)
if (leftCamera != null)
{
{
MainCamera = newCamera;
LeftCamera = leftCamera;
//open a session
//open a session
SendSDKCommand(delegate { Error = EDSDK.EdsOpenSession(MainCamera.Ref); });
SendSDKCommand(delegate { LeftError = EDSDK.EdsOpenSession(LeftCamera.Ref); });
//subscribe to the camera events (for the SDK)
//subscribe to the camera events (for the SDK)
EDSDK.EdsSetCameraStateEventHandler(MainCamera.Ref, EDSDK.StateEvent_All, SDKStateEvent, MainCamera.Ref);
EDSDK.EdsSetCameraStateEventHandler(LeftCamera.Ref, EDSDK.StateEvent_All, SDKStateEvent, LeftCamera.Ref);
EDSDK.EdsSetObjectEventHandler(MainCamera.Ref, EDSDK.ObjectEvent_All, SDKObjectEvent, MainCamera.Ref);
EDSDK.EdsSetObjectEventHandler(LeftCamera.Ref, EDSDK.ObjectEvent_All, SDKObjectEvent, LeftCamera.Ref);
EDSDK.EdsSetPropertyEventHandler(MainCamera.Ref, EDSDK.PropertyEvent_All, SDKPropertyEvent, MainCamera.Ref);
EDSDK.EdsSetPropertyEventHandler(LeftCamera.Ref, EDSDK.PropertyEvent_All, SDKPropertyEvent, LeftCamera.Ref);
CameraSessionOpen = true;
LeftCameraSessionOpen = true;
}
if (RightCameraSessionOpen) CloseRightSession();
if (rightCamera != null)
{
RightCamera = rightCamera;
//open a session
SendSDKCommand(delegate { RightError = EDSDK.EdsOpenSession(RightCamera.Ref); });
//subscribe to the camera events (for the SDK)
EDSDK.EdsSetCameraStateEventHandler(RightCamera.Ref, EDSDK.StateEvent_All, SDKStateEvent, RightCamera.Ref);
EDSDK.EdsSetObjectEventHandler(RightCamera.Ref, EDSDK.ObjectEvent_All, SDKObjectEvent, RightCamera.Ref);
EDSDK.EdsSetPropertyEventHandler(RightCamera.Ref, EDSDK.PropertyEvent_All, SDKPropertyEvent, RightCamera.Ref);
RightCameraSessionOpen = true;
}
}
}
}


/// <summary>
/// <summary>
/// Closes the session with the current camera
/// Closes the session with the current camera
/// </summary>
/// </summary>
public void CloseSession()
public void CloseLeftSession()
{
{
if (CameraSessionOpen)
if (LeftCameraSessionOpen)
{
{
//if live view is still on, stop it and wait till the thread has stopped
//if live view is still on, stop it and wait till the thread has stopped
if (IsLiveViewOn)
if (IsLeftLiveViewOn)
{
{
StopLiveView();
StopLeftLiveView();
LVThread.Join(1000);
Left_LVThread.Join(1000);
}
}


//Remove the event handler
//Remove the event handler
EDSDK.EdsSetCameraStateEventHandler(MainCamera.Ref, EDSDK.StateEvent_All, null, MainCamera.Ref);
EDSDK.EdsSetCameraStateEventHandler(LeftCamera.Ref, EDSDK.StateEvent_All, null, LeftCamera.Ref);
EDSDK.EdsSetObjectEventHandler(MainCamera.Ref, EDSDK.ObjectEvent_All, null, MainCamera.Ref);
EDSDK.EdsSetObjectEventHandler(LeftCamera.Ref, EDSDK.ObjectEvent_All, null, LeftCamera.Ref);
EDSDK.EdsSetPropertyEventHandler(MainCamera.Ref, EDSDK.PropertyEvent_All, null, MainCamera.Ref);
EDSDK.EdsSetPropertyEventHandler(LeftCamera.Ref, EDSDK.PropertyEvent_All, null, LeftCamera.Ref);


//close session and release camera
//close session and release camera
SendSDKCommand(delegate { Error = EDSDK.EdsCloseSession(MainCamera.Ref); });
SendSDKCommand(delegate { LeftError = EDSDK.EdsCloseSession(LeftCamera.Ref); });
uint c = EDSDK.EdsRelease(MainCamera.Ref);
uint c = EDSDK.EdsRelease(LeftCamera.Ref);
CameraSessionOpen = false;
LeftCameraSessionOpen = false;
}
}
public void CloseRightSession()
{
if (RightCameraSessionOpen)
{
//if live view is still on, stop it and wait till the thread has stopped
if (IsRightLiveViewOn)
{
StopRightLiveView();
Right_LVThread.Join(1000);
}

//Remove the event handler
EDSDK.EdsSetCameraStateEventHandler(RightCamera.Ref, EDSDK.StateEvent_All, null, RightCamera.Ref);
EDSDK.EdsSetObjectEventHandler(RightCamera.Ref, EDSDK.ObjectEvent_All, null, RightCamera.Ref);
EDSDK.EdsSetPropertyEventHandler(RightCamera.Ref, EDSDK.PropertyEvent_All, null, RightCamera.Ref);

//close session and release camera
SendSDKCommand(delegate { RightError = EDSDK.EdsCloseSession(RightCamera.Ref); });
uint c = EDSDK.EdsRelease(RightCamera.Ref);
RightCameraSessionOpen = false;
}
}
}
}


/// <summary>
/// <summary>
/// Closes open session and terminates the SDK
/// Closes open session and terminates the SDK
/// </summary>
/// </summary>
public void Dispose()
public void Dispose()
{
{
//close session
//close session
CloseSession();
CloseLeftSession();
CloseRightSession();
//terminate SDK
//terminate SDK
Error = EDSDK.EdsTerminateSDK();
LeftError = EDSDK.EdsTerminateSDK();
RightError = LeftError;
//stop command execution thread
//stop command execution thread
STAThread.Shutdown();
STAThread.Shutdown();
}
}


#endregion
#endregion


#region Eventhandling
#region Eventhandling


/// <summary>
/// <summary>
/// A new camera was plugged into the computer
/// A new camera was plugged into the computer
/// </summary>
/// </summary>
/// <param name="inContext">The pointer to the added camera</param>
/// <param name="inContext">The pointer to the added camera</param>
/// <returns>An EDSDK errorcode</returns>
/// <returns>An EDSDK errorcode</returns>
private uint SDKHandler_CameraAddedEvent(IntPtr inContext)
private uint SDKHandler_CameraAddedEvent(IntPtr inContext)
{
{
//Handle new camera here
//Handle new camera here
if (CameraAdded != null) CameraAdded();
if (CameraAdded != null) CameraAdded();
return EDSDK.EDS_ERR_OK;
return EDSDK.EDS_ERR_OK;
}
}


/// <summary>
/// <summary>
/// An Objectevent fired
/// An Objectevent fired
/// </summary>
/// </summary>
/// <param name="inEvent">The ObjectEvent id</param>
/// <param name="inEvent">The ObjectEvent id</param>
/// <param name="inRef">Pointer to the object</param>
/// <param name="inRef">Pointer to the object</param>
/// <param name="inContext"></param>
/// <param name="inContext"></param>
/// <returns>An EDSDK errorcode</returns>
/// <returns>An EDSDK errorcode</returns>
private uint Camera_SDKObjectEvent(uint inEvent, IntPtr inRef, IntPtr inContext)
private uint Camera_SDKObjectEvent(uint inEvent, IntPtr inRef, IntPtr inContext)
{
{
//handle object event here
//handle object event here
switch (inEvent)
switch (inEvent)
{
{
case EDSDK.ObjectEvent_All:
case EDSDK.ObjectEvent_All:
break;
break;
case EDSDK.ObjectEvent_DirItemCancelTransferDT:
case EDSDK.ObjectEvent_DirItemCancelTransferDT:
break;
break;
case EDSDK.ObjectEvent_DirItemContentChanged:
case EDSDK.ObjectEvent_DirItemContentChanged:
break;
break;
case EDSDK.ObjectEvent_DirItemCreated:
case EDSDK.ObjectEvent_DirItemCreated:
if (DownloadVideo) { DownloadImage(inRef, ImageSaveDirectory); DownloadVideo = false; }
if (Left_DownloadVideo) { DownloadImage(inRef, LeftImageSaveDirectory); Left_DownloadVideo = false; }
if (Right_DownloadVideo) { DownloadImage(inRef, RightImageSaveDirectory); Right_DownloadVideo = false; }
break;
break;
case EDSDK.ObjectEvent_DirItemInfoChanged:
case EDSDK.ObjectEvent_DirItemInfoChanged:
break;
break;
case EDSDK.ObjectEvent_DirItemRemoved:
case EDSDK.ObjectEvent_DirItemRemoved:
break;
break;
case EDSDK.ObjectEvent_DirItemRequestTransfer:
case EDSDK.ObjectEvent_DirItemRequestTransfer:
DownloadImage(inRef, ImageSaveDirectory);
DownloadImage(inRef, LeftImageSaveDirectory);
DownloadImage(inRef, RightImageSaveDirectory);
break;
break;
case EDSDK.ObjectEvent_DirItemRequestTransferDT:
case EDSDK.ObjectEvent_DirItemRequestTransferDT:
break;
break;
case EDSDK.ObjectEvent_FolderUpdateItems:
case EDSDK.ObjectEvent_FolderUpdateItems:
break;
break;
case EDSDK.ObjectEvent_VolumeAdded:
case EDSDK.ObjectEvent_VolumeAdded:
break;
break;
case EDSDK.ObjectEvent_VolumeInfoChanged:
case EDSDK.ObjectEvent_VolumeInfoChanged:
break;
break;
case EDSDK.ObjectEvent_VolumeRemoved:
case EDSDK.ObjectEvent_VolumeRemoved:
break;
break;
case EDSDK.ObjectEvent_VolumeUpdateItems:
case EDSDK.ObjectEvent_VolumeUpdateItems:
break;
break;
}
}


return EDSDK.EDS_ERR_OK;
return EDSDK.EDS_ERR_OK;
}
}


/// <summary>
/// <summary>
/// A progress was made
/// A progress was made
/// </summary>
/// </summary>
/// <param name="inPercent">Percent of progress</param>
/// <param name="inPercent">Percent of progress</param>
/// <param name="inContext">...</param>
/// <param name="inContext">...</param>
/// <param name="outCancel">Set true to cancel event</param>
/// <param name="outCancel">Set true to cancel event</param>
/// <returns>An EDSDK errorcode</returns>
/// <returns>An EDSDK errorcode</returns>
private uint Camera_SDKProgressCallbackEvent(uint inPercent, IntPtr inContext, ref bool outCancel)
private uint Camera_SDKProgressCallbackEvent(uint inPercent, IntPtr inContext, ref bool outCancel)
{
{
//Handle progress here
//Handle progress here
if (ProgressChanged != null) ProgressChanged((int)inPercent);
if (ProgressChanged != null) ProgressChanged((int)inPercent);
return EDSDK.EDS_ERR_OK;
return EDSDK.EDS_ERR_OK;
}
}


/// <summary>
/// <summary>
/// A property changed
/// A property changed
/// </summary>
/// </summary>
/// <param name="inEvent">The PropetyEvent ID</param>
/// <param name="inEvent">The PropetyEvent ID</param>
/// <param name="inPropertyID">The Property ID</param>
/// <param name="inPropertyID">The Property ID</param>
/// <param name="inParameter">Event Parameter</param>
/// <param name="inParameter">Event Parameter</param>
/// <param name="inContext">...</param>
/// <param name="inContext">...</param>
/// <returns>An EDSDK errorcode</returns>
/// <returns>An EDSDK errorcode</returns>
private uint Camera_SDKPropertyEvent(uint inEvent, uint inPropertyID, uint inParameter, IntPtr inContext)
private uint Camera_SDKPropertyEvent(uint inEvent, uint inPropertyID, uint inParameter, IntPtr inContext)
{
{
//Handle property event here
//Handle property event here
switch (inEvent)
switch (inEvent)
{
{
case EDSDK.PropertyEvent_All:
case EDSDK.PropertyEvent_All:
break;
break;
case EDSDK.PropertyEvent_PropertyChanged:
case EDSDK.PropertyEvent_PropertyChanged:
break;
break;
case EDSDK.PropertyEvent_PropertyDescChanged:
case EDSDK.PropertyEvent_PropertyDescChanged:
break;
break;
}
}


switch (inPropertyID)
switch (inPropertyID)
{
{
case EDSDK.PropID_AEBracket:
case EDSDK.PropID_AEBracket:
break;
break;
case EDSDK.PropID_AEMode:
case EDSDK.PropID_AEMode:
break;
break;
case EDSDK.PropID_AEModeSelect:
case EDSDK.PropID_AEModeSelect:
break;
break;
case EDSDK.PropID_AFMode:
case EDSDK.PropID_AFMode:
break;
break;
case EDSDK.PropID_Artist:
case EDSDK.PropID_Artist:
break;
break;
case EDSDK.PropID_AtCapture_Flag:
case EDSDK.PropID_AtCapture_Flag:
break;
break;
case EDSDK.PropID_Av:
case EDSDK.PropID_Av:
break;
break;
case EDSDK.PropID_AvailableShots:
case EDSDK.PropID_AvailableShots:
break;
break;
case EDSDK.PropID_BatteryLevel:
case EDSDK.PropID_BatteryLevel:
break;
break;
case EDSDK.PropID_BatteryQuality:
case EDSDK.PropID_BatteryQuality:
break;
break;
case EDSDK.PropID_BodyIDEx:
case EDSDK.PropID_BodyIDEx:
break;
break;
case EDSDK.PropID_Bracket:
case EDSDK.PropID_Bracket:
break;
break;
case EDSDK.PropID_CFn:
case EDSDK.PropID_CFn:
break;
break;
case EDSDK.PropID_ClickWBPoint:
case EDSDK.PropID_ClickWBPoint:
break;
break;
case EDSDK.PropID_ColorMatrix:
case EDSDK.PropID_ColorMatrix:
break;
break;
case EDSDK.PropID_ColorSaturation:
case EDSDK.PropID_ColorSaturation:
break;
break;
case EDSDK.PropID_ColorSpace:
case EDSDK.PropID_ColorSpace:
break;
break;
case EDSDK.PropID_ColorTemperature:
case EDSDK.PropID_ColorTemperature:
break;
break;
case EDSDK.PropID_ColorTone:
case EDSDK.PropID_ColorTone:
break;
break;
case EDSDK.PropID_Contrast:
case EDSDK.PropID_Contrast:
break;
break;
case EDSDK.PropID_Copyright:
case EDSDK.PropID_Copyright:
break;
break;
case EDSDK.PropID_DateTime:
case EDSDK.PropID_DateTime:
break;
break;
case EDSDK.PropID_DepthOfField:
case EDSDK.PropID_DepthOfField:
break;
break;
case EDSDK.PropID_DigitalExposure:
case EDSDK.PropID_DigitalExposure:
break;
break;
case EDSDK.PropID_DriveMode:
case EDSDK.PropID_DriveMode:
break;
break;
case EDSDK.PropID_EFCompensation:
case EDSDK.PropID_EFCompensation:
break;
break;
case EDSDK.PropID_Evf_AFMode:
case EDSDK.PropID_Evf_AFMode:
break;
break;
case EDSDK.PropID_Evf_ColorTemperature:
case EDSDK.PropID_Evf_ColorTemperature:
break;
break;
case EDSDK.PropID_Evf_DepthOfFieldPreview:
case EDSDK.PropID_Evf_DepthOfFieldPreview:
break;
break;
case EDSDK.PropID_Evf_FocusAid:
case EDSDK.PropID_Evf_FocusAid:
break;
break;
case EDSDK.PropID_Evf_Histogram:
case EDSDK.PropID_Evf_Histogram:
break;
break;
case EDSDK.PropID_Evf_HistogramStatus:
case EDSDK.PropID_Evf_HistogramStatus:
break;
break;
case EDSDK.PropID_Evf_ImagePosition:
case EDSDK.PropID_Evf_ImagePosition:
break;
break;
case EDSDK.PropID_Evf_Mode:
case EDSDK.PropID_Evf_Mode:
break;
break;
case EDSDK.PropID_Evf_OutputDevice:
case EDSDK.PropID_Evf_OutputDevice:
if (IsLiveViewOn == true) DownloadEvf();
if (IsLeftLiveViewOn || IsRightLiveViewOn) DownloadEvf();
break;
break;
case EDSDK.PropID_Evf_WhiteBalance:
case EDSDK.PropID_Evf_WhiteBalance:
break;
break;
case EDSDK.PropID_Evf_Zoom:
case EDSDK.PropID_Evf_Zoom:
break;
break;
case EDSDK.PropID_Evf_ZoomPosition:
case EDSDK.PropID_Evf_ZoomPosition:
break;
break;
case EDSDK.PropID_ExposureCompensation:
case EDSDK.PropID_ExposureCompensation:
break;
break;
case EDSDK.PropID_FEBracket:
case EDSDK.PropID_FEBracket:
break;
break;
case EDSDK.PropID_FilterEffect:
case EDSDK.PropID_FilterEffect:
break;
break;
case EDSDK.PropID_FirmwareVersion:
case EDSDK.PropID_FirmwareVersion:
break;
break;
case EDSDK.PropID_FlashCompensation:
case EDSDK.PropID_FlashCompensation:
break;
break;
case EDSDK.PropID_FlashMode:
case EDSDK.PropID_FlashMode:
break;
break;
case EDSDK.PropID_FlashOn:
case EDSDK.PropID_FlashOn:
break;
break;
case EDSDK.PropID_FocalLength:
case EDSDK.PropID_FocalLength:
break;
break;
case EDSDK.PropID_FocusInfo:
case EDSDK.PropID_FocusInfo:
break;
break;
case EDSDK.PropID_GPSAltitude:
case EDSDK.PropID_GPSAltitude:
break;
break;
case EDSDK.PropID_GPSAltitudeRef:
case EDSDK.PropID_GPSAltitudeRef:
break;
break;
case EDSDK.PropID_GPSDateStamp:
case EDSDK.PropID_GPSDateStamp:
break;
break;
case EDSDK.PropID_GPSLatitude:
case EDSDK.PropID_GPSLatitude:
break;
break;
case EDSDK.PropID_GPSLatitudeRef:
case EDSDK.PropID_GPSLatitudeRef:
break;
break;
case EDSDK.PropID_GPSLongitude:
case EDSDK.PropID_GPSLongitude:
break;
break;
case EDSDK.PropID_GPSLongitudeRef:
case EDSDK.PropID_GPSLongitudeRef:
break;
break;
case EDSDK.PropID_GPSMapDatum:
case EDSDK.PropID_GPSMapDatum:
break;
break;
case EDSDK.PropID_GPSSatellites:
case EDSDK.PropID_GPSSatellites:
break;
break;
case EDSDK.PropID_GPSStatus:
case EDSDK.PropID_GPSStatus:
break;
break;
case EDSDK.PropID_GPSTimeStamp:
case EDSDK.PropID_GPSTimeStamp:
break;
break;
case EDSDK.PropID_GPSVersionID:
case EDSDK.PropID_GPSVersionID:
break;
break;
case EDSDK.PropID_HDDirectoryStructure:
case EDSDK.PropID_HDDirectoryStructure:
break;
break;
case EDSDK.PropID_ICCProfile:
case EDSDK.PropID_ICCProfile:
break;
break;
case EDSDK.PropID_ImageQuality:
case EDSDK.PropID_ImageQuality:
break;
break;
case EDSDK.PropID_ISOBracket:
case EDSDK.PropID_ISOBracket:
break;
break;
case EDSDK.PropID_ISOSpeed:
case EDSDK.PropID_ISOSpeed:
break;
break;
case EDSDK.PropID_JpegQuality:
case EDSDK.PropID_JpegQuality:
break;
break;
case EDSDK.PropID_LensName:
case EDSDK.PropID_LensName:
break;
break;
case EDSDK.PropID_LensStatus:
case EDSDK.PropID_LensStatus:
break;
break;
case EDSDK.PropID_Linear:
case EDSDK.PropID_Linear:
break;
break;
case EDSDK.PropID_MakerName:
case EDSDK.PropID_MakerName:
break;
break;
case EDSDK.PropID_MeteringMode:
case EDSDK.PropID_MeteringMode:
break;
break;
case EDSDK.PropID_NoiseReduction:
case EDSDK.PropID_NoiseReduction:
break;
case EDSDK.PropID_Orientation:
break;
case EDSDK.PropID_OwnerName:
break;
case EDSDK.PropID_ParameterSet:
break;
case EDSDK.PropID_PhotoEffect:
break;
case EDSDK.PropID_PictureStyle:
break;
case EDSDK.PropID_PictureStyleCaption:
break;
case EDSDK.PropID_PictureStyleDesc:
break;
case EDSDK.PropID_ProductName:
break;
case EDSDK.PropID_Record:
break;
case EDSDK.PropID_RedEye:
break;
case EDSDK.PropID_SaveTo:
break;
case EDSDK.PropID_Sharpness:
break;
case EDSDK.PropID_ToneCurve:
break;
case EDSDK.PropID_ToningEffect:
break;
case EDSDK.PropID_Tv:
break;
case EDSDK.PropID_Unknown:
break;
case EDSDK.PropID_WBCoeffs:
break;
case EDSDK.PropID_WhiteBalance:
break;
case EDSDK.PropID_WhiteBalanceBracket:
break;
case EDSDK.PropID_WhiteBalanceShift:
break;
}
return EDSDK.EDS_ERR_OK;
}

/// <summary>
/// The camera state changed
/// </summary>
/// <param name="inEvent">The StateEvent ID</param>
/// <param name="inParameter">Parameter from this event</param>
/// <param name="inContext">...</param>
/// <returns>An EDSDK errorcode</returns>
private uint Camera_SDKStateEvent(uint inEvent, uint inParameter, IntPtr inContext)
{
//Handle state event here
switch (inEvent)
{
case EDSDK.StateEvent_All:
break;
case EDSDK.StateEvent_AfResult:
break;
case EDSDK.StateEvent_BulbExposureTime:
break;
case EDSDK.StateEvent_CaptureError:
break;
case EDSDK.StateEvent_InternalError:
break;
case EDSDK.StateEvent_JobStatusChanged:
break;
case EDSDK.StateEvent_Shutdown:
CameraSessionOpen = false;
if (LVThread.IsAlive) LVThread.Abort();
if (CameraHasShutdown != null) CameraHasShutdown(this, new EventArgs());
break;
case EDSDK.StateEvent_ShutDownTimerUpdate:
break;
case EDSDK.StateEvent_WillSoonShutDown:
break;
}
return EDSDK.EDS_ERR_OK;
}

#endregion

#region Camera commands

#region Download data

/// <summary>
/// Downloads an image to given directory
/// </summary>
/// <param name="ObjectPointer">Pointer to the object. Get it from the SDKObjectEvent.</param>
/// <param name="directory">Path to where the image will be saved to</param>
public void DownloadImage(IntPtr ObjectPointer, string directory)
{
EDSDK.EdsDirectoryItemInfo dirInfo;
IntPtr streamRef;
//get information about object
Error = EDSDK.EdsGetDirectoryItemInfo(ObjectPointer, out dirInfo);
string CurrentPhoto = Path.Combine(directory, dirInfo.szFileName);

SendSDKCommand(delegate
{
//create filestream to data
Error = EDSDK.EdsCreateFileStream(CurrentPhoto, EDSDK.EdsFileCreateDisposition.CreateAlways, EDSDK.EdsAccess.ReadWrite, out streamRef);
//download file
lock (STAThread.ExecLock) { DownloadData(ObjectPointer, streamRef); }
//release stream
Error = EDSDK.EdsRelease(streamRef);
}, true);
}

/// <summary>
/// Downloads a jpg image from the camera into a Bitmap. Fires the ImageDownloaded event when done.
/// </summary>
/// <param name="ObjectPointer">Pointer to the object. Get it from the SDKObjectEvent.</param>
public void DownloadImage(IntPtr ObjectPointer)
{
//get information about image
EDSDK.EdsDirectoryItemInfo dirInfo = new EDSDK.EdsDirectoryItemInfo();
Error = EDSDK.EdsGetDirectoryItemInfo(ObjectPointer, out dirInfo);

//check the extension. Raw data cannot be read by the bitmap class
string ext = Path.GetExtension(dirInfo.szFileName).ToLower();
if (ext == ".jpg" || ext == ".jpeg")
{
SendSDKCommand(delegate
{
Bitmap bmp = null;
IntPtr streamRef, jpgPointer = IntPtr.Zero;
uint length = 0;

//create memory stream
Error = EDSDK.EdsCreateMemoryStream(dirInfo.Size, out streamRef);

//download data to the stream
lock (STAThread.ExecLock) { DownloadData(ObjectPointer, streamRef); }
Error = EDSDK.EdsGetPointer(streamRef, out jpgPointer);
Error = EDSDK.EdsGetLength(streamRef, out length);

unsafe
{
//create a System.IO.Stream from the pointer
using (UnmanagedMemoryStream ums = new UnmanagedMemoryStream((byte*)jpgPointer.ToPointer(), length, length, FileAccess.Read))
{
//create bitmap from stream (it's a normal jpeg image)
bmp = new Bitmap(ums);
}
}

//release data
Error = EDSDK.EdsRelease(streamRef);

//Fire the event with the image
if (ImageDownloaded != null) ImageDownloaded(bmp);
}, true);
}
else
{
//if it's a RAW image, cancel the download and release the image
SendSDKCommand(delegate { Error = EDSDK.EdsDownloadCancel(ObjectPointer); });
Error = EDSDK.EdsRelease(ObjectPointer);
}
}

/// <summary>
/// Gets the thumbnail of an image (can be raw or jpg)
/// </summary>
/// <param name="filepath">The filename of the image</param>
/// <returns>The thumbnail of the image</returns>
public Bitmap GetFileThumb(string filepath)
{
IntPtr stream;
//create a