2011-07-25 10 views
1

私のC#アプリケーションは3分ごとに画像を撮影しており、毎回期待どおりにEDSDKから画像を取得しています。問題はEDSDKであることを非常に確実に確認しています。Canon EDSDK ObjectEvent_DirItemRequestTransfer

コード:

private uint CameraObjectEvent(uint inEvent, IntPtr inRef, IntPtr inContext) 
    { 
     switch (inEvent) 
     { 
      case EDSDK.ObjectEvent_DirItemRequestTransfer: 
       GetCapturedItem(inRef); 
       break; 
     } 

     return EDSDKErrorCodes.EDS_ERR_OK; 
    } 

    private void GetCapturedItem(IntPtr directoryItem) 
    { 
     uint error = EDSDKErrorCodes.EDS_ERR_OK; 
     IntPtr stream = IntPtr.Zero; 

     //Get information of the directory item. 
     EDSDK.EdsDirectoryItemInfo dirItemInfo; 
     error = EDSDK.EdsGetDirectoryItemInfo(directoryItem, out dirItemInfo); 
     if (error != EDSDKErrorCodes.EDS_ERR_OK) 
     { 
      OnCameraErrorRaised(error, "EDSDK.EdsGetDirectoryItemInfo failed."); 
      return; 
     } 

     //Create a file stream for receiving image. 
     error = EDSDK.EdsCreateMemoryStream(dirItemInfo.Size, out stream); 
     if (error != EDSDKErrorCodes.EDS_ERR_OK) 
     { 
      OnCameraErrorRaised(error, "EDSDK.EdsCreateMemoryStream failed"); 
      return; 
     } 

     //Fill the stream with the resulting image 
     error = EDSDK.EdsDownload(directoryItem, dirItemInfo.Size, stream); 
     if (error != EDSDKErrorCodes.EDS_ERR_OK) 
     { 
      OnCameraErrorRaised(error, "EDSDK.EdsDownload failed."); 
      return; 
     } 

     error = EDSDK.EdsDownloadComplete(directoryItem); 
     if (error != EDSDKErrorCodes.EDS_ERR_OK) 
     { 
      OnCameraErrorRaised(error, "EDSDK.EdsDownloadComplete failed."); 
      return; 
     } 

     //Copy the stream to a byte[] 
     IntPtr pointerToBytes = IntPtr.Zero; 
     EDSDK.EdsGetPointer(stream, out pointerToBytes); 

     MemoryStream imageStream = null; 
     Image image = null; 

     try 
     { 
      byte[] buffer = new byte[dirItemInfo.Size]; 
      GCHandle gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); 
      Marshal.Copy(pointerToBytes, buffer, 0, (int)dirItemInfo.Size); 

      //Create a MemoryStream and return the image 
      imageStream = new MemoryStream(buffer); 
      image = Image.FromStream(imageStream); 
     } 
     catch (Exception ex) 
     { 
      OnCameraErrorRaised(999999, string.Format("Failed while retrieving image from camera. Exception: {0}.", ex.Message)); 
     } 
     finally 
     { 
      if (imageStream != null) 
       imageStream.Dispose(); 
     } 

     //If image was captured then send ImageCaptured event 
     if (image != null) 
      OnImageCaptured(image); 

     //Clean up 
     EDSDK.EdsRelease(pointerToBytes); 
     pointerToBytes = IntPtr.Zero; 

     EDSDK.EdsRelease(stream); 
     stream = IntPtr.Zero; 

     EDSDK.EdsRelease(directoryItem); 
     directoryItem = IntPtr.Zero; 
    } 

OnImageCaptured(画像)だけで、別の画像とカメラから画像をマージした後、フィナーレマージされた画像を保存した後、両方の画像を配置コントローラに画像を送信する行:

private void ImageCaptured(Image originalImage) 
    { 
     Image watermark = null; 

     //Merge images 
     try 
     { 
      watermark = Image.FromFile(Settings.Default.ImageWatermarkFilename); 
      _imageController.Merge(originalImage, watermark); 
      _imageController.SaveImage(originalImage); 
     } 
     catch (Exception ex) 
     { 
      LogManager.Instance.UpdateLog(string.Format("Error - Failed to merge and save images. Exception: {0}.", ex.Message)); 

      //HACK: 
      System.Windows.Forms.Application.Restart(); 
      App.Current.Shutdown(); 
     } 
     finally 
     { 
      originalImage.Dispose(); 
      if (watermark != null) 
       watermark.Dispose(); 
     } 
    } 

だから、なぜアプリケーションのメモリリーク - アイデアですか?

/乾杯

+0

を取っているたびに犯人食べメモリである: - 非常に単純な私は問題を解決した...私はちょうどgcHandle.Free()を呼び出す必要がありました。 – Weed2k

答えて

0

あなたのGCHandleをリリースしてください。それはあなたが更新撮影

gcHandle.Free()

関連する問題