2013-02-25 89 views
0

我有一個16位的位圖。我想在我的ASP.NET端以8bpp圖像轉換該圖像。在C#中將位圖從16bpp轉換爲8bpp

我嘗試了很多選項,我在互聯網上找到了,但沒有爲我工作。

我也試過這樣:C# Converting 32bpp image to 8bpp

但如果我要保存的文件,我得到以下錯誤:

異常詳細信息:System.Runtime.InteropServices.ExternalException:發生了一般性錯誤GDI +。

Line 278:     System.Drawing.Image img2 = Convert(bm_resize);//byteArrayToImage(gray); 
Line 279:      
Line 280:     img2.Save(helper+"grey2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 
Line 281:    } 
Line 282: 

對我的問題有沒有正確的方法?

全碼:

System.Drawing.Image img2 = Convert(bm_resize); 
img2.Save(path+"test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 


public static System.Drawing.Image Convert(Bitmap oldbmp) 
    { 
     using (MemoryStream ms = new MemoryStream()) 
     { 
      oldbmp.Save(ms, ImageFormat.Gif); 
      ms.Position = 0; 
      return System.Drawing.Image.FromStream(ms); 
     } 
    } 
+0

的[裝入從流圖像而不保持流開放]可能重複(http://stackoverflow.com/questions/3845456/loading-an-image-from-a-stream-without-keeping-the-流打開) – mbeckish 2013-02-25 15:45:52

回答

1

問題是由於在保存圖像之前丟棄了內存流而造成的。

我相信GDI +需要內存流持續下去,而你還在從內存流創建映像工作。

Microsoft Support article.

+0

感謝您的幫助。 你可以看看我的答案。 – 2013-02-26 06:54:47

+0

@ user694501 - 您希望我看的答案在哪裏? – mbeckish 2013-02-26 13:40:09

0

AForge.Net具有的這些變化和更多的自由(下次級GPL許可證)例程好好珍藏。將16bpp轉換爲8bpp與this一樣簡單。