GDI+中发生一般性错误的解决办法

阅读更多
这个错误经常发生,代码如下:
private  static  byte[] GetBytes (Image image)  
     {  
         try  
         {  
             if (image == null) return null;  
             using (MemoryStream stream = new MemoryStream())  
                 {  
                     image .Save(stream, ImageFormat.Jpeg);  
                     return stream.GetBuffer();  
                 }  
         }  
         finally  
         {  
             if(image != null)  
             {  
                 image.Dispose();  
                 image = null;  
             }  
         }  
     }  


修改后的代码如下:
private  static  byte[] GetBytes (Image image)  
     {  
         try  
         {  
             if (image == null) return null;  
             using(Bitmap bitmap = new Bitmap(image))  
             {  
                 using (MemoryStream stream = new MemoryStream())  
                 {  
                     bitmap.Save(stream, ImageFormat.Jpeg);  
                     return stream.GetBuffer();  
                 }  
             }  
         }  
         finally  
         {  
             if(image != null)  
             {  
                 image.Dispose();  
                 image = null;  
            }  
         }  
     }  


转自:http://blog.csdn.net/chenlycly/article/details/42560127
另可参考:https://www.cnblogs.com/moy-1313133/p/6734393.html

你可能感兴趣的:(c#)