2012-01-12 246 views
2

可能重複:
「Parameter not valid」 exception loading System.Drawing.ImageSystem.ArgumentException:參數無效

我將在一個數據庫中的圖像。

這裏是我的代碼

public class ImageUtils 
{ 
    const int sizeThumb = 69; 

    public static int uploadImage(int memberid, Image thumbimage) 
    { 
     MemoryStream stream = new MemoryStream(); 
     thumbimage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp); 
     Byte[] thumbbytes = stream.ToArray(); 

     //int length = Convert.ToInt32(data.Length); 
     //byte[] thumbimage = new byte[length]; 
     //data.Read(thumbimage, 0, length); 
     SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["FMMImages"].ConnectionString); 
     SqlCommand command = new SqlCommand("update Images_temp set thumbimage = @thumbimage where [email protected]", connection); 
     SqlParameter param0 = new SqlParameter("@thumbimage", SqlDbType.Image); 
     param0.Value = thumbbytes; 
     command.Parameters.Add(param0); 

     connection.Open(); 

     object result = command.ExecuteScalar(); 
     connection.Close(); 
     if (result != null) 
     { 
      return System.Convert.ToInt32(result); 
     } 
     else 
     { 
      return 0; 
     } 
    } 

aspx.cs在那裏我打電話uploadimage 圖像CroppedWaterMarkImage ......

ImageUtils.uploadImage(memberid, CroppedWaterMarkImage); 

錯誤在uploadimage功能:

 MemoryStream stream = new MemoryStream(); 
    thumbimage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp); 
    Byte[] thumbbytes = stream.ToArray(); 

System.ArgumentException:參數無效。

感謝 孫

+0

如果您調試,它停在哪條線上? – Joe 2012-01-12 23:29:19

+0

如果C#數據庫庫與Java類似,那麼您是不是將「thumbimage」傳遞給'SqlParameter'構造函數而不是「@thumbimage」?另外,您似乎沒有爲「@memberid」參數設置值。 – aroth 2012-01-12 23:30:20

+0

http://stackoverflow.com/questions/629955/parameter-not-valid-exception-loading-system-drawing-image – 2012-01-12 23:31:07

回答

1

這些人遇到了類似的問題,圖像和的MemoryStream(),因爲內存泄漏:

這種聯繫是通過調用解決System.Drawing.Bitmap而不是System.Drawing.Image:

或者(內存泄漏/損壞和/或API的選擇),可以適用於您的方案。

還要確保圖像文件有效。