public byte[] DownloadLastPhoto() { int count; uint err; IntPtr?dirInfo = getDCIMFolder(); IntPtr folderHandle; IntPtr imageHandle; EDSDK.EdsDirectoryItemInfo imageInfo; IntPtr memStream; IntPtr streamStart; //MAKE SURE WE HAVE A DCIM FOLDER if (dirInfo == null) { if (errorEvent != null) { errorEvent("Failed To Find DCIM Folder"); } return(null); } err = EDSDK.EdsGetChildCount(dirInfo.Value, out count); if (err != EDSDK.EDS_ERR_OK) { if (errorEvent != null) { errorEvent("Failed To Get Child Folder!"); } return(null); } err = EDSDK.EdsGetChildAtIndex(dirInfo.Value, count - 1, out folderHandle); err = EDSDK.EdsGetChildCount(folderHandle, out count); err = EDSDK.EdsGetChildAtIndex(folderHandle, count - 1, out imageHandle); err = EDSDK.EdsGetDirectoryItemInfo(imageHandle, out imageInfo); err = EDSDK.EdsCreateMemoryStream(0, out memStream); EDSDK.EdsDownload(imageHandle, imageInfo.Size, memStream); EDSDK.EdsDownloadComplete(imageHandle); byte[] imageData = new byte[imageInfo.Size]; EDSDK.EdsGetPointer(memStream, out streamStart); Marshal.Copy(streamStart, imageData, 0, (int)imageInfo.Size); EDSDK.EdsRelease(streamStart); EDSDK.EdsRelease(memStream); EDSDK.EdsRelease(folderHandle); EDSDK.EdsRelease(imageHandle); EDSDK.EdsRelease(dirInfo.Value); DownloadingPhoto = false; return(imageData); }
/// <summary> /// Download image to disk. /// </summary> /// <param name="dirRef">A reference to objects created by the event</param> private void DownloadImage(IntPtr dirRef) { IntPtr stream = IntPtr.Zero; IntPtr data = IntPtr.Zero; EdsDirectoryItemInfo dirItemInfo; try { UInt32 returnValue = EDSDK.EdsGetDirectoryItemInfo(dirRef, out dirItemInfo); ReturnValueManager.HandleFunctionReturnValue(returnValue); string fullpath = String.Empty; if (!String.IsNullOrEmpty(ImageSaveDirectory)) { fullpath = System.IO.Path.Combine(ImageSaveDirectory, dirItemInfo.szFileName); } else { fullpath = System.IO.Path.Combine(Environment.CurrentDirectory, dirItemInfo.szFileName); } returnValue = EDSDK.EdsCreateFileStream(fullpath, EdsFileCreateDisposition.CreateAlways, EdsAccess.ReadWrite, out stream); ReturnValueManager.HandleFunctionReturnValue(returnValue); returnValue = EDSDK.EdsDownload(dirRef, dirItemInfo.Size, stream); ReturnValueManager.HandleFunctionReturnValue(returnValue); if (returnValue == (uint )ReturnValue.Ok) { returnValue = EDSDK.EdsDownloadComplete(dirRef); } else { returnValue = EDSDK.EdsDownloadCancel(dirRef); } returnValue = EDSDK.EdsGetPointer(stream, out data); ReturnValueManager.HandleFunctionReturnValue(returnValue); } catch (Exception ex) { Console.Out.WriteLine(ex.Message); } finally { EDSDK.EdsRelease(stream); EDSDK.EdsRelease(data); } }
private ImageFile GetImageFile(IntPtr inRef, Folder aFolder) { EDSDK.EdsDirectoryItemInfo directoryItemInfo = GetDirectoryItemInfo(inRef); string filename = aFolder.ComposeFilename(directoryItemInfo.szFileName); IntPtr stream = GetFileStream(filename); try { SDKHelper.CheckError(EDSDK.EdsDownload(inRef, directoryItemInfo.Size, stream)); SDKHelper.CheckError(EDSDK.EdsDownloadComplete(inRef)); } catch { SDKHelper.CheckError(EDSDK.EdsDownloadCancel(inRef)); throw; } finally { SDKHelper.CheckError(EDSDK.EdsRelease(stream)); } return(new ImageFile(File.ReadAllBytes(filename), directoryItemInfo.szFileName)); }
public static unsafe void DownloadImage(IntPtr[] camera, int Count) { IntPtr sdRef = IntPtr.Zero; IntPtr dicmRef = IntPtr.Zero; IntPtr canonRef = IntPtr.Zero; int nFiles; IniFile iniFile = new IniFile(configForm.iniFilePaths); for (int i = 0; i < Count; i++) { EDSDK.EdsGetChildAtIndex(camera[i], 0, out sdRef); EDSDK.EdsGetChildAtIndex(sdRef, 0, out dicmRef); EDSDK.EdsGetChildAtIndex(dicmRef, 0, out canonRef); EDSDK.EdsGetChildCount(canonRef, out nFiles); string dest = PicDest; //for (int i = 0; i < 5; i++) { IntPtr fileRef = IntPtr.Zero; EDSDK.EdsDirectoryItemInfo fileInfo; #region Get Directory and download try { EDSDK.EdsGetChildAtIndex(canonRef, (nFiles - 1), out fileRef); EDSDK.EdsGetDirectoryItemInfo(fileRef, out fileInfo); IntPtr fStream = IntPtr.Zero; //it's a cannon sdk file stream, not a managed stream uint fSize = fileInfo.Size; #region DownLoad try { EDSDK.EdsCreateMemoryStream(fSize, out fStream); EDSDK.EdsDownload(fileRef, fSize, fStream); EDSDK.EdsDownloadComplete(fileRef); byte[] buffer = new byte[fSize]; IntPtr imgLocation; //uint _imageLen; if (EDSDK.EdsGetPointer(fStream, out imgLocation) == EDSDK.EDS_ERR_OK) { #region save Marshal.Copy(imgLocation, buffer, 0, (int)fSize - 1); EDSDK.EdsDeviceInfo deviceInfo; EDSDK.EdsGetDeviceInfo(camera[i], out deviceInfo); string deviceName; deviceName = iniFile.IniReadValue("CameraID", deviceInfo.szPortName); File.WriteAllBytes(dest + deviceName + "__" + fileInfo.szFileName, buffer); //EDSDK.EdsGetLength(fStream, out _imageLen); //UnmanagedMemoryStream ums = new UnmanagedMemoryStream((byte*)imgLocation.ToPointer(), _imageLen, _imageLen, FileAccess.Read); //Bitmap _bmp = new Bitmap(ums, true); //pictureBox1.Image = _bmp; //_bmp.Save("new1", System.Drawing.Imaging.ImageFormat.Jpeg); #endregion } } finally { EDSDK.EdsRelease(fStream); Console.WriteLine("DownloadSuccess\t" + i.ToString()); } #endregion } finally { EDSDK.EdsRelease(fileRef); } #endregion } } }