2015-10-03 51 views
0

我有類似:銷燬流造成破壞的位圖

 public Bitmap GetBitmap() 
     {   
      Byte[] byteArray= bring it from somewhere  
      using (Stream stream = new MemoryStream(byteArray)) 
      { 
       return new Bitmap(stream); 
      } 
     } 

當我用這個方法外的位圖被壓碎。但如果我進入「使用」範圍,位圖將會存在並且工作正常。似乎配置流導致配置位圖.. 的問題是: 我需要一些深層複製嗎?我該如何執行它?

回答

1

當你處置Bitmap將會丟失,所以你確實需要這麼做一個深層複製。 最終,你的代碼應該是:

public static Bitmap GetBitmap() 
{ 
    byte[] byteArray = bring it from somewhere 
    using (Stream stream = new MemoryStream(byteArray)) 
    { 
     var tempBitmap = new Bitmap(stream); 
     return new Bitmap(tempBitmap); // This will deep-copy the Bitmap 
    } 
} 

順便說一句,平時基本類型,像byte都寫在小的情況下。

+0

是的,工作...感謝字節說明 –