2016-10-24 5 views
0

BitmapImage (System.Windows.Media.Imaging)를 memorystream을 통해 저장하려고하므로 결과를 사용하여 비트 맵 (System.Drawing)을 만들 수 있습니다. MemoryStream을로 인코딩 된 결과를 저장하려고 할 때MemoryStream으로 저장할 때 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 X 500 그렇게 엄청난 수 없습니다. 어떤 아이디어가 원인 일 수 있습니까? 또는 성능을 저하시키지 않고 memorystream을 사용하지 않고 변환 작업을 수행 할 수있는 다른 방법은 무엇입니까?

답변

0

위의 코드에서 볼 수없는 croppedbitmap의 원래 소스 비트 맵 이미지가 문제를 일으키는 것으로 나타났습니다.

이 비트 맵 이미지는 URI를 통해 디스크에서로드되었습니다. BitmapCacheOption을 None 대신 Onload으로 설정하면 오류가 제거되는 것을 발견했습니다. 나는 이미지가 변환 시점에 완전히로드되지 않았다고 생각합니다. 그러나 성능이 3 배 정도 저하되며 이는 불행한 일입니다.