2016-10-24 21 views
0

メモリマップを介してBitmapImage(System.Windows.Media.Imaging)を保存しようとしているため、その結果を使ってビットマップ(System.Drawing)を作成できます。 MemoryStreamにエンコードされた結果を保存しようとしたときにMemorymapとして保存するとBitmapImage AccessViolationExceptionが発生する

私は断続的にエラーが発生します。

An exception of type 'System.AccessViolationException' occurred in PresentationCore.dll but was not handled in user code 

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. 

をMemoryStreamをのプロパティは、読み取りや書き込みのタイムアウトが発生したことを示しているように見えます。

WriteTimeout = 'msOut.WriteTimeout' threw an exception of type 'System.InvalidOperationException' 

以下のコードは、エラーが保存コマンドでスローされます。問題の

  System.Windows.Media.Imaging.CroppedBitmap cbi = new System.Windows.Media.Imaging.CroppedBitmap(bi, new System.Windows.Int32Rect(
      (int)(imageViewBox[2] * imageViewBox[10]), (int)(imageViewBox[3] * imageViewBox[11]), 
      (int)((imageViewBox[4] - imageViewBox[2]) * imageViewBox[10]), (int)((imageViewBox[5] - imageViewBox[3]) * imageViewBox[11]))); 
     newImageSize = new Size(cbi.PixelWidth, cbi.PixelHeight); 

     using (MemoryStream msOut = new MemoryStream()) 
     { 
      System.Windows.Media.Imaging.BmpBitmapEncoder enc = new System.Windows.Media.Imaging.BmpBitmapEncoder(); 
      enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(cbi)); 
      // Throws access violation exception when zoomed on some images. Why? 
      enc.Save(msOut); 
      using (Bitmap temp = new Bitmap(msOut)) 
      { ... 

画像は、一般的に1000px×500それほど大規模ではありません。 これを引き起こす原因は何か? (性能を低下させることなく)変換を行うことができたのですか?

答えて

0

croppedbitmap(上記のコードには表示されていません)の元のソースビットマップイメージが原因でした。

このビットマップイメージは、URI経由でディスクからロードされました。 BitmapCacheOptionをNoneの代わりにOnloadに設定すると、エラーが取り除かれています。私は画像が変換の時点で完全に読み込まれていないと推測しています。しかし、パフォーマンスが3倍に低下しますが、残念です。

関連する問題