2013-01-16 80 views
1

我已經將圖像上傳到我的數據庫中作爲byte[],現在我試圖將其展示出來。將字節[]轉換爲圖像

發生錯誤 - 參數異常是由用戶代碼Parameter is not valid

未處理在該線

newImage = System.Drawing.Image.FromStream(stream); 

這是我的代碼

for (int i = 0; i < topRatingList.Count; i++) 
      { 
       int commentRating = topRatingList[i].CommentRating; 
       int drinkID = topRatingList[i].DrinkID; 

       if (i == 0) 
       { 
        DrinkMenuDAO drink = DrinkMenuBLL.getDrinkMenu(drinkID); 
        LinkButton1.Text = drink.DrinkName; 
        ImageButton1.ImageUrl = byteArrayToImage(drink.DrinkImage); 
       } 
      } 


private string byteArrayToImage(byte[] byteArrayIn) 
    { 
     System.Drawing.Image newImage; 
     string strFileName = Server.MapPath("~/Temp/images/") + ".jpg"; 
     if (byteArrayIn != null) 
     { 
      using (MemoryStream stream = new MemoryStream(byteArrayIn)) 
      { 
       newImage = System.Drawing.Image.FromStream(stream); 
       newImage.Save(strFileName); 
      } 
      return strFileName; 
     } 
     else 
     { 
      return ""; 
     } 
    } 

,我使用的代碼存儲圖像

protected void bn_upload_Click(object sender, EventArgs e) 
    { 
     lbl_msg.Text = ""; 
     Stream imgStream = FileUpload1.PostedFile.InputStream; 
     BinaryReader imgBinary = new BinaryReader(imgStream); 
     bytes = imgBinary.ReadBytes((Int32)imgStream.Length); 
     imgBinary.Close(); 
     imgStream.Close(); 
     string src = byteArrayToImage(bytes); 
     if (src.Equals("")) 
     { 
     } 
     else 
     { 
      Image1.ImageUrl = "~/Temp/UploadedImage.jpg"; 
     } 
    } 

    private string byteArrayToImage(byte[] byteArrayIn) 
    { 
     System.Drawing.Image newImage; 
     string strFileName = Server.MapPath("~/Temp/") + "UploadedImage.jpg"; 
     if (byteArrayIn != null) 
     { 
      using (MemoryStream stream = new MemoryStream(byteArrayIn)) 
      { 
       newImage = System.Drawing.Image.FromStream(stream); 
       newImage.Save(strFileName); 
      } 
      return strFileName; 
     } 
     else 
     { 
      return ""; 
     } 
    } 

    protected void btn_submit_Click(object sender, EventArgs e) 
    { 
     DrinkMenuBLL.uploadImg(bytes); 
     lbl_msg.Text = "uploaded"; 
    } 

我在數據庫中使用varbinary(MAX)和我用

public static void UploadImg(byte[] drinkImage) 
    { 

     SqlConnection con = new SqlConnection(Constring.getConString()); 

     string sql = "Update DrinkMenu set drinkImage = (@imgData) where drinkID = 4"; 

     SqlCommand cmd = new SqlCommand(sql, con); 
     cmd.Parameters.Add("@imgData", SqlDbType.Binary).Value = drinkImage; 
     try 
     { 
      con.Open(); 
      cmd.ExecuteNonQuery(); 
     } 
     finally 
     { 
      con.Close(); 
      cmd.Dispose(); 
      con.Dispose(); 
     } 
    } 
+4

你確定你的'byte []'代表圖像正確嗎?我們可以看到存儲它的代碼嗎? – bluevector

+1

bytes = imgBinary.ReadBytes((Int32)imgStream.Length); 這不是讀取文件的好方法。喬恩Skeet在這裏有一個很好的答案:http://stackoverflow.com/questions/221925/creating-a-byte-array-from-a-stream(他曾經有一個優秀的博客文章討論各種方法,但我可以' t找到它)。 – Pete

回答

1

一個問題的SQL可能是下列之一:

bytes = imgBinary.ReadBytes((Int32)imgStream.Length); 

如果流的長度超過的Int32。 MaxValue,因爲它是一個Int64?也許你正在截斷你的圖像...使用這種方法,而不是讀取流緩衝區:

public static Byte[] ReadStream(Stream input) 
{ 
    Byte[] buffer = new Byte[(16 * 1024)]; 

    using (MemoryStream stream = new MemoryStream()) 
    { 
     Int32 read; 

     while ((read = input.Read(buffer, 0, buffer.Length)) > 0) 
      stream.Write(buffer, 0, read); 

     return stream.ToArray(); 
    } 
} 

protected void bn_upload_Click(object sender, EventArgs e) 
{ 
    lbl_msg.Text = ""; 

    Byte[] bytes = ReadStream(FileUpload1.PostedFile.InputStream); 
    String src = byteArrayToImage(bytes); 

    if (!src.Equals("")) 
     Image1.ImageUrl = "~/Temp/UploadedImage.jpg"; 
} 
+0

sry但我相當瞭解它..我應該如何使用它? – sihao

+0

給我一分鐘,我將它應用到你的代碼... –

+0

嗯,我在哪裏看過那個代碼? – Pete