2012-05-01 41 views
2

每個人,在將圖片保存到媒體庫時遇到了一個奇怪的問題,我的應用程序崩潰而未出現異常。這是我的保存代碼。WP7試圖創建WriteableBitmap實例時出現崩潰

using (MemoryStream stream = new MemoryStream()) 
{ 
    try 
    { 
     WriteableBitmap bitmap = new WriteableBitmap(InkPrest, InkPrest.RenderTransform); // Crash here, the actualHeight of InkPrest is 2370.0 

     bitmap.SaveJpeg(stream, (int)InkPrest.ActualWidth, (int)InkPrest.ActualHeight, 0, 100); 
     stream.Seek(0, SeekOrigin.Begin); 

     MediaLibrary library = new MediaLibrary(); 
     library.SavePicture(DateTime.Now.ToString(), stream.GetBuffer()); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

我已經debuged一步一個腳印,在

WriteableBitmap bitmap = new WriteableBitmap(InkPrest, InkPrest.RenderTransform); // Crash here, the actualHeight of InkPrest is 2370.0 

解決這個問題的任何想法的應用程序崩潰?

============================================

嘗試保存多個圖像

的UIElement是704 * 2370

TranslateTransform transform = new TranslateTransform(); 
transform.Transform(new Point(0,0)); 
double MaxHeight = 800; 
double height = InkPrest.ActualHeight; 
int saveCount = 0; 
int succeedCount = 0; 
while (height > 0) 
{ 
    using (MemoryStream stream = new MemoryStream()) 
    { 
     try 
     { 
      double actualRenderHeight = Math.Min(height, MaxHeight); 
      WriteableBitmap bitmap = new WriteableBitmap((int)InkPrest.ActualWidth, (int)actualRenderHeight); 

      bitmap.Render(InkPrest, transform); //Crash here, also no exception. 
      bitmap.Invalidate(); 

      height -= actualRenderHeight; 
      transform.Y -= actualRenderHeight; 

      bitmap.SaveJpeg(stream, (int)InkPrest.ActualWidth, (int)actualRenderHeight, 0, 100); 
      stream.Seek(0, SeekOrigin.Begin); 

      MediaLibrary library = new MediaLibrary(); 
      Picture pic = library.SavePicture(manuscriptFile.Title + DateTime.Now.ToString(), stream.GetBuffer()); 
      saveCount++; 
      if (pic != null) 
      { 
       succeedCount++; 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 
} 
+0

這是在模擬器上,還是隻在真實的設備上? –

+0

都在仿真器和設備上。崩潰沒有例外。 – Wp7Beginner

+0

我想碰撞共振是我的圖片比2000年更久。我正在測試它。 – Wp7Beginner

回答

0

確定你是否在uithread或一些其他創建的線程WriteableBitmap的實例。你需要在uithread中創建writeablebitmap。