2014-10-20 49 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

相关问题