2017-04-17 31 views
0

我在數據庫中有字節翻譯的圖像。而且我有這些圖像的標籤。 用戶需要通過在列表框中輸入所需的標籤來搜索圖像。 PictureBox必須創建到最多的圖片數量。從數據庫中取出一個字節圖像

失敗的錯誤消息

An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll 
Additional information: Parameter is not valid. 

這裏是我的代碼;

PictureBox[] img = new PictureBox[9999]; 
      for (int j = 0; j < listBox1.Items.Count; j++) 
      { 
       con.Open(); 
       MySqlCommand cmdImgCount = new MySqlCommand("select count(scan.image) from deu_scanner.scan where scan.id_Image IN (select kw_img.FK_idImg from deu_scanner.kw_img where kw_img.FK_idKeyword IN (select keyword.idkeyword from deu_scanner.keyword where keyword.keywordName='" + listBox1.Items[j] + "'));", con); 
       imgCount = Convert.ToInt32(cmdImgCount.ExecuteScalar().ToString()); 
       con.Close(); 
       ArrayList ar = new ArrayList(); 

       for (int i = 0; i < imgCount; i++) 
       { 
        con.Open(); 
        MySqlCommand cmd = new MySqlCommand("select scan.image from deu_scanner.scan where scan.id_Image IN (select kw_img.FK_idImg from deu_scanner.kw_img where kw_img.FK_idKeyword IN (select keyword.idkeyword from deu_scanner.keyword where keyword.keywordName='" + listBox1.Items[j] + "'))", con); 
        MySqlDataReader dr = cmd.ExecuteReader(); 
        while (dr.Read()) 
        { 
         byte[] imagedata = (byte[])dr["image"]; 
         MemoryStream memorystream = new MemoryStream(imagedata, 0, imagedata.Length); 
         memorystream.Write(imagedata, 0, imagedata.Length); 
         memorystream.Position = 0; 
         Image sourceImg = Image.FromStream(memorystream, true); 
         clonedImg = new Bitmap(sourceImg.Width, sourceImg.Height); 
         var copy = Graphics.FromImage(clonedImg); 
         copy.DrawImage(sourceImg, 0, 0); 
         ar.Add(clonedImg); 
        } 

        con.Close(); 
       } 
       for (int k = 0; k < imgCount; k++) 
       { 
        img[k] = new PictureBox(); 
        img[k].Name = "image-" + k.ToString(); 
        img[k].Image = (Image)ar[k]; 
        img[k].Visible = true; 
        img[k].SizeMode = PictureBoxSizeMode.StretchImage; 
        img[k].SetBounds(12 + k * 150, 180, 120, 120); 
        this.Controls.Add(img[k]); 
        img[k].BringToFront(); 
       } 
} 

更新(從註釋):該數據庫填充有該代碼

SaveFileDialog save = new SaveFileDialog(); 
save.Filter = "JPEG(.JPG)|.jpg"; 
FileStream fs = new FileStream(save.FileName, FileMode.Open, FileAccess.Read); 
BinaryReader br = new BinaryReader(fs); 
byte[] data = br.ReadBytes(Convert.ToInt32(fs.Length)); 
br.Close(); 
fs.Close(); 
+1

它在哪裏失敗? – thst

+0

這裏; Image sourceImg = Image.FromStream(memorystream,true); @thst – DilaraD

+0

[使用實體框架6從SQL Server保存並檢索圖像(二進制)]的可能副本(http://stackoverflow.com/questions/25400555/save-and-retrieve-image-binary-from-sql-server -using-entity-framework-6) –

回答

0

Image的-object可以直接從一個流中讀取JPEG。然而,如果它拋出一個ArgumentExceptiondotnet documents說,圖像被打破格式。

檢查DB內容:你應該測試你從一個獨立的JPG觀衆數據庫中讀取內容:從數據庫中加載數據,並將其寫入文件,然後測試該文件是一個有效的JPG 。由於您使用的是mysql,因此您可以使用mysql工作臺工具或HeidiSQL直接從表中保存blob數據,而不會出現任何可能存在問題的代碼。

完成之後,請確保JPG可讀。 如果沒有,請檢查編寫代碼。在你的數據庫上傳

可能的問題:FileStream.length屬性賦予,而不是文件大小的長度。所以,數據庫的內容可能不完整。

我不會重複SO,所以here is the way to properly copy the stream爲數據庫的字節數組。

你應該這樣某物結束:

FileStream fs = new FileStream(save.FileName, FileMode.Open, FileAccess.Read); 
MemoryStream memorystream = new MemoryStream(); 
fs.CopyTo(memorystream); 
byte[] data = memorystream.ToArray(); 
// close streams 

data現在包含選定文件中的所有字節。

我放棄了所有的安全措施,如文件存在等,這是留給OP :-)

+0

謝謝,我正在等待@thst的詳細解答 – DilaraD

相關問題