Esempio n. 1
0
        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();
            }
        }