2014-10-20 47 views
0

我想從圖像創建一個字節數組,以便將數據存儲在一個blob在MySQL數據庫中。 有沒有必要告訴我,Blob是一個不好的做法等等,我們已經談過了,在我們的案例中使用什麼爭論,因爲它只是一個小圖像文件,然後Blob是要走的路。 這是我的代碼:通用GDI +錯誤發生在ConvertTo

ImageConverter converter = new ImageConverter(); 
byte[] byteImg = (byte[])converter.ConvertTo(original, typeof(byte[])); 

我正在使用C#和Winforms。 我不斷得到一個通用的gdi +錯誤,original位圖圖像無法訪問。 爲了檢查這個問題,我在ImageConverter行之前插入了一行代碼,在一個picturebox中顯示original位圖,並且顯示正確,因此我認爲訪問沒有問題。 我在這裏查找所有可能的答案在stackoverflow(他們是很多),他們都要求檢查我是否處置圖像,如果它不再有效等等。 我沒有using在我的代碼也沒有處理圖像。 original實際上是我嘗試訪問的全局變量。

**編輯 我試圖將圖像存儲在一個圖片盒中,並將它從圖片盒中轉換出來,它仍然給出了相同的錯誤。 該異常基本上是ObjectDisposedException,但我不會將其置於我的代碼中的任何位置。 如果它有助於它與我使用的是不同的大小

**編輯2 發佈一套完整的代碼 Bitmap resizedImage, original; 所有圖像發生了,我宣佈這兩個任何方法之外的全局變量。 在下面的代碼中,我讀取了原始圖像,將其存儲在位圖中,並創建了一個新的調整後的圖像,以滿足我的需求,同時保持縱橫比。

private void imageResize() 
     { 
      if (string.IsNullOrWhiteSpace(imageLabel.Text)) 
      { 
       flag = false; 
       imageLabel.BackColor = Color.FromArgb(252, 240, 109); 
      } 
      else 
      { 
       try 
       { 

        using (FileStream fs = new System.IO.FileStream(imageLabel.Text, System.IO.FileMode.Open)) 
        { 

         original = new Bitmap(fs); 
        } 

        int rectHeight = 100; 
        int rectWidth = 112; 
        if (original.Height == original.Width) 
        { 
         resizedImage = new Bitmap(original, rectHeight, rectHeight); 
        } 
        else 
        { 
         float aspect = original.Width/(float)original.Height; 
         int newWidth, newHeight; 
         newWidth = (int)(rectWidth * aspect); 
         newHeight = (int)(newWidth/aspect); 

         if (newWidth > rectWidth || newHeight > rectHeight) 
         { 
          if (newWidth > newHeight) 
          { 
           newWidth = rectWidth; 
           newHeight = (int)(newWidth/aspect); 

          } 
          else 
          { 
           newHeight = rectHeight; 
           newWidth = (int)(newHeight * aspect); 

          } 
         } 
         resizedImage = new Bitmap(original, newWidth, newHeight); 



        } 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show("Not supported file type " + ex.Message); 
       } 
      } 
     } 

當用戶點擊上傳到數據庫按鈕時,它會調用imageResize方法,然後再調用下一個發送數據。

private void saveData() 
     { 

      //code for reading rest of the data. 
      //Flag=false if something is wrong with it. Removed it as it has nothing to do 
      // with the error. Only string types here 
      if (flag) 
      { 
       DB.Image = original; 
       MessageBox.Show("pause"); 
       MySqlConnection con = new MySqlConnection("server=127.0.0.1;database=testrmaomo;user id=root;password=root;persistsecurityinfo=True"); 
       con.Open(); 
       MySqlCommand cmd; 
       //convert image to byte array 
       ImageConverter converter = new ImageConverter(); 
       byte[] byteImg = (byte[])converter.ConvertTo(original.Clone(), typeof(byte[])); 
       byte[] byteImgPrint = (byte[])converter.ConvertTo(resizedImage.Clone(), typeof(byte[])); 

       //check if entry already exists in DB 
       cmd = new MySqlCommand("SELECT count(*) FROM logopath;", con); 
       StringBuilder sb = new StringBuilder(); 
       //check if the record exists. If it does update it if not insert it. 
       // Removed as it has nothing to do with the error 
      } 

發生錯誤後,只是爲了看看原來仍佔據圖像中添加的DB.Image=original;行代碼。它確實如此。

**編輯3我找到了解決我的問題,通過轉換爲字節數組,例如

 ImageConverter converter = new ImageConverter(); 
     Bitmap tmp = new Bitmap(original); 
     byte[] byteImg = (byte[])converter.ConvertTo(tmp.Clone(), typeof(byte[])); 

之前創建一個臨時位圖或我想我做的,因爲使用.Clone()偶爾之前固定我的問題。 問題是爲什麼?

+0

請問,如果你使用'original.Clone()它的工作'? – DavidG 2014-10-20 11:45:40

+0

是的。謝謝您的幫助 – 2014-10-20 11:47:13

+0

@DavidG測試後仍然發生。克隆它使它偶爾不會發生....... – 2014-10-20 11:50:18

回答

1

您可能需要克隆位圖並使用它:

byte[] byteImg = (byte[])converter.ConvertTo(original.Clone(), typeof(byte[])); 
+0

當然最大的問題是爲什麼? – 2014-10-20 11:48:36

+0

@PatrickHofman我同意,我覺得有點不舒服,把這個作爲一個答案沒有解釋,但我不認爲我們可以知道爲什麼沒有更多的上下文。 – DavidG 2014-10-20 11:49:50

+0

@DavidG測試後仍然發生。克隆它偶爾不會發生..... – 2014-10-20 11:50:42

相關問題