2015-10-28 37 views
5

SoftwareBitmapWriteableBitmapに変換するたびに、次の例外が発生します。 System.Runtime.InteropServices.COMExceptionここでSoftwareBitmapをWriteableBitmapに変換する

は、そのための私のコードスニペットです:

private async void Start(object sender, RoutedEventArgs e) 
     { 

      _MediaCapture = new MediaCapture(); 
      await _MediaCapture.InitializeAsync(); 

      mediaElement.Source = _MediaCapture; 
      await _MediaCapture.StartPreviewAsync(); 
      DispatcherTimer timer = new DispatcherTimer(); 
      timer.Interval = new TimeSpan(0, 0, 0, 1); 
      timer.Tick += HandleTimerTick; 
      timer.Start(); 
     } 

     private async void HandleTimerTick(object Sender, object E) 
     { 


      var frame = await _MediaCapture.GetPreviewFrameAsync(); 
      SoftwareBitmap frameBitmap = frame.SoftwareBitmap; 
      WriteableBitmap bitmap = new WriteableBitmap(frameBitmap.PixelWidth, frameBitmap.PixelHeight); 
      try 
      { 
       frameBitmap.CopyToBuffer(bitmap.PixelBuffer); 
      } 
      catch (Exception) 
      { 
       Debug.WriteLine("Exception "); 
      } 
     } 

ライン

frameBitmap.CopyToBuffer(bitmap.PixelBuffer); 

は例外をスローしています。

これはx64 RemoteDeviceでデバッグしています。

+0

は例外が何を投げていますか? –

+0

@Dmitry Bychenko上記を参照してください:System.Runtime.InteropServices.COMException – TheTanic

+0

サイドノート:「catch(Exception){Debug.WriteLine( "Exception");}のような例外をスワッピングします。 } 'は非常に悪い練習です*。 –

答えて

6

コードを使用してこの問題を再現できます。 frame.SoftwareBitmapは常にnullを返します。

あなたは、次のようなコードを使用してこの問題を解決することができます

private async void button_Click(object sender, RoutedEventArgs e) 
    { 
     _mediaCapture = new MediaCapture(); 

     await _mediaCapture.InitializeAsync(); 

     mediaElement.Source = _mediaCapture; 

     await _mediaCapture.StartPreviewAsync(); 

     DispatcherTimer timer = new DispatcherTimer(); 
     timer.Interval = new TimeSpan(0, 0, 0, 1); 
     timer.Tick += Timer_Tick; 
     timer.Start(); 
    } 

    private async void Timer_Tick(object sender, object e) 
    { 
     var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties; 

     var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height); 

     var frame = await _mediaCapture.GetPreviewFrameAsync(videoFrame); 

     SoftwareBitmap frameBitmap = frame.SoftwareBitmap; 

     WriteableBitmap bitmap = new WriteableBitmap(frameBitmap.PixelWidth, frameBitmap.PixelHeight); 

     frameBitmap.CopyToBuffer(bitmap.PixelBuffer); 

     Debug.WriteLine("done"); 
    } 
+0

うまく動作します。ありがとうございます – TheTanic

+0

@ジェフリー私はこれがどのように役立つか分かりません...唯一の違いはGetPreviewFrameAsyncの戻り値を使用しているようです。それは意味をなさない!その関数は、渡されたVideoFrameを使って埋め尽くさないのですか?もしそうなら、それは異なって文書化されるべきです。 VideoFrameよりもInitializeAsyncで別のビットマップピクセルフォーマットを指定した場合、ヒットした場合はヒットしますか? –

+0

@ジェフリークリアするには...うまくいきます!私はなぜちょうど混乱しています。 –

関連する問題