public void DownloadLiveViewImage(Action <MemoryStream, uint> onImageRecieved) { IntPtr stream = IntPtr.Zero; IntPtr image = IntPtr.Zero; IntPtr pointer = IntPtr.Zero; uint size = 0; try { SDKHelper.CheckError(EDSDK.EdsCreateMemoryStream(BUFFER_SIZE, out stream)); SDKHelper.CheckError(EDSDK.EdsCreateEvfImageRef(stream, out image)); SDKHelper.CheckError(EDSDK.EdsDownloadEvfImage(_pointer, image)); SDKHelper.CheckError(EDSDK.EdsGetPointer(stream, out pointer)); SDKHelper.CheckError(EDSDK.EdsGetLength(stream, out size)); onImageRecieved(CopyToMemoryStream(pointer, size), size); SDKHelper.CheckError(EDSDK.EdsRelease(image)); SDKHelper.CheckError(EDSDK.EdsRelease(stream)); } catch (SDKComeBackLaterException) { } }
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); }
public unsafe static Bitmap DownloadEvf(IntPtr Camera) { uint success; Bitmap _bmp, _newbmp; UnmanagedMemoryStream ums; // while (true) { IntPtr _stream = IntPtr.Zero; IntPtr _image = new IntPtr(); IntPtr _imageData = new IntPtr(); uint _imageLen; int _width = 500, _height = 330; _newbmp = new Bitmap(_width, _height); success = EDSDK.EdsCreateMemoryStream(0, out _stream); if (success == EDSDK.EDS_ERR_OK) { success = EDSDK.EdsCreateEvfImageRef(_stream, out _image); } if (success == EDSDK.EDS_ERR_OK) { success = EDSDK.EdsDownloadEvfImage(Camera, _image); } if (success == EDSDK.EDS_ERR_OK) { EDSDK.EdsGetPointer(_stream, out _imageData); EDSDK.EdsGetLength(_stream, out _imageLen); ums = new UnmanagedMemoryStream((byte *)_imageData.ToPointer(), _imageLen, _imageLen, FileAccess.Read); _bmp = new Bitmap(ums, true); _newbmp = new Bitmap(_bmp, _width, _height); //LiveViewBox是定义的一个pictureBox 控件 //LiveViewBox1.Image = _newbmp; } if (_stream != IntPtr.Zero) { EDSDK.EdsRelease(_stream); _stream = IntPtr.Zero; } if (_image != IntPtr.Zero) { EDSDK.EdsRelease(_image); _image = IntPtr.Zero; } } return(_newbmp); }
public unsafe static Bitmap DownloadPreview(IntPtr Camera) { uint success; Bitmap _bmp, _newbmp; UnmanagedMemoryStream ums; //while (true) { IntPtr _stream = IntPtr.Zero; IntPtr _image = new IntPtr(); IntPtr _imageData = new IntPtr(); uint _imageLen; int _width = 1050, _height = 700; _newbmp = new Bitmap(_width, _height); success = EDSDK.EdsCreateMemoryStream(0, out _stream); if (success == EDSDK.EDS_ERR_OK) { success = EDSDK.EdsCreateEvfImageRef(_stream, out _image); } if (success == EDSDK.EDS_ERR_OK) { success = EDSDK.EdsDownloadEvfImage(Camera, _image); } if (success == EDSDK.EDS_ERR_OK) { EDSDK.EdsGetPointer(_stream, out _imageData); EDSDK.EdsGetLength(_stream, out _imageLen); ums = new UnmanagedMemoryStream((byte *)_imageData.ToPointer(), _imageLen, _imageLen, FileAccess.Read); _bmp = new Bitmap(ums, true); _newbmp = new Bitmap(_bmp, _width, _height); Console.WriteLine("bmp is download"); } if (_stream != IntPtr.Zero) { EDSDK.EdsRelease(_stream); _stream = IntPtr.Zero; Console.WriteLine("relase stream"); } if (_image != IntPtr.Zero) { EDSDK.EdsRelease(_image); _image = IntPtr.Zero; Console.WriteLine("relase image"); } } return(_newbmp); }
public Stream GetLiveViewImage() { IntPtr streamPointer = IntPtr.Zero; IntPtr imagePointer = IntPtr.Zero; UInt32 returnValue = EDSDK.EdsCreateMemoryStream(0, out streamPointer); ReturnValueManager.HandleFunctionReturnValue(returnValue); try { returnValue = EDSDK.EdsCreateEvfImageRef(streamPointer, out imagePointer); ReturnValueManager.HandleFunctionReturnValue(returnValue); try { returnValue = EDSDK.EdsDownloadEvfImage(this.Handle, imagePointer); ReturnValueManager.HandleFunctionReturnValue(returnValue); IntPtr imageBlob; returnValue = EDSDK.EdsGetPointer(streamPointer, out imageBlob); ReturnValueManager.HandleFunctionReturnValue(returnValue); try { ulong imageBlobLength; returnValue = EDSDK.EdsGetLength(streamPointer, out imageBlobLength); ReturnValueManager.HandleFunctionReturnValue(returnValue); byte[] buffer = new byte[imageBlobLength]; Marshal.Copy(imageBlob, buffer, 0, (int)imageBlobLength); Stream stream = new MemoryStream(buffer); return(stream); } finally { EDSDK.EdsRelease(imageBlob); } } finally { EDSDK.EdsRelease(imagePointer); } } finally { EDSDK.EdsRelease(streamPointer); } }
/// <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); } }
public void updatePic() { EDSDK.EdsDownloadEvfImage(this.CameraPtr, this.EvfImgPtr); IntPtr ptr; EDSDK.EdsGetPointer(MemStreamPtr, out ptr); uint len; EDSDK.EdsGetLength(MemStreamPtr, out len); Byte[] data = new Byte[len]; Marshal.Copy(ptr, data, 0, (int)len); System.IO.MemoryStream memStream = new System.IO.MemoryStream(data); bmp.BeginInit(); bmp.StreamSource = memStream; bmp.EndInit(); memStream.Dispose(); EDSDK.EdsRelease(ptr); }
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 } } }
private void savePictureToHost(IntPtr ptr, string path) { Canon_EOS_Remote.classes.Image tmpImage; tmpImage = new classes.Image(ptr); Byte[] byteArray = new byte[(int)tmpImage.ImageItemInfo.Size]; uint error = 0; IntPtr outputStream; error = EDSDK.EdsCreateMemoryStream((uint)tmpImage.ImageItemInfo.Size, out outputStream); if (error != 0) { Console.WriteLine("Error at creating file stream : " + ErrorCodes.getErrorDataWithCodeNumber(error)); } error = EDSDK.EdsDownload(ptr, (uint)tmpImage.ImageItemInfo.Size, outputStream); if (error != 0) { Console.WriteLine("Error at download : " + ErrorCodes.getErrorDataWithCodeNumber(error)); } IntPtr imageRef = IntPtr.Zero; error = EDSDK.EdsCreateImageRef(outputStream, out imageRef); if (error != 0) { Console.WriteLine("Error at createimageref : " + ErrorCodes.getErrorDataWithCodeNumber(error)); } EDSDK.EdsImageInfo imageinfo; error = EDSDK.EdsGetImageInfo(imageRef, EDSDK.EdsImageSource.FullView, out imageinfo); if (error != 0) { Console.WriteLine("Error at getiamgeinfo : " + ErrorCodes.getErrorDataWithCodeNumber(error)); } error = EDSDK.EdsRelease(imageRef); if (error != 0) { Console.WriteLine("Error at release imageref : " + ErrorCodes.getErrorDataWithCodeNumber(error)); } GCHandle gcHandle = GCHandle.Alloc(byteArray, GCHandleType.Pinned); IntPtr adress = gcHandle.AddrOfPinnedObject(); IntPtr streamRef = IntPtr.Zero; error = EDSDK.EdsGetPointer(outputStream, out streamRef); if (error != 0) { Console.WriteLine("Error at getpointer : " + ErrorCodes.getErrorDataWithCodeNumber(error)); } Marshal.Copy(streamRef, byteArray, 0, (int)tmpImage.ImageItemInfo.Size); try { FileStream fstream = new FileStream(path + tmpImage.ImageItemInfo.szFileName, FileMode.Create); fstream.Write(byteArray, 0, byteArray.Length); fstream.Close(); } catch (UnauthorizedAccessException e) { Console.WriteLine("Zugriff verweigert."); System.Windows.MessageBox.Show("Zugriff verweigert : \n" + e.Message); } catch (Exception e) { Console.WriteLine("Exception at filestream : " + e.Message); } finally { error = EDSDK.EdsRelease(outputStream); if (error != 0) { Console.WriteLine("Error at release outputstream : " + ErrorCodes.getErrorDataWithCodeNumber(error)); } error = EDSDK.EdsRelease(streamRef); if (error != 0) { Console.WriteLine("Error at at release streamref : " + ErrorCodes.getErrorDataWithCodeNumber(error)); } gcHandle.Free(); } }