【问题标题】:A generic error occurred in GDI+ when update更新时 GDI+ 中发生一般错误
【发布时间】:2017-04-25 20:35:28
【问题描述】:

当我更新表单时,只有当我不更新(更改)图像时才会出现此错误。
当我用新图像更新时,我没有这个错误。

Try
    Dim mstream As New System.IO.MemoryStream()
    PictureBox1.Image = New Bitmap(Image.FromStream(mstream))
    Dim arrImage() As Byte = mstream.GetBuffer()
    mstream.Close()

    MysqlConn.Close()
    MysqlConn.Open()
    COMMAND.Connection = MysqlConn
    COMMAND.CommandText = "update product set 
        id_maker=@Value1, 
        foto=@foto  
        where id = '" & TextBox1.Text & "'"
    COMMAND.Parameters.AddWithValue("@Value1", If(String.IsNullOrEmpty(ComboBox1.Text), DBNull.Value, ComboBox1.Text))

    COMMAND.Parameters.AddWithValue("@foto", arrImage)
    READER = COMMAND.ExecuteReader
    MessageBox.Show("Datos Guardados")
    MysqlConn.Close()
Catch ex As Exception
    MessageBox.Show(ex.Message)
Finally
    MysqlConn.Dispose()
End Try
MysqlConn.Close()

【问题讨论】:

  • 我完全不明白你的代码是如何工作的。在第一行中,您正在初始化一个没有给定任何参数的新 MemoryStream,然后立即尝试从中获取图像?另请确定导致异常的确切代码行,并发布完整的异常详细信息。
  • 你是什么意思when i dont update(change) the image 你的代码显示图像数据无论如何都在传递。除了上述代码之外,代码还有很多错误 a) 不要使用GetBuffer(),使用ToArray - 考虑根本不保存图像,而是保存到某个存档位置的文件名; b) 不要使用 AddWithValue() 使用 Add(string, dbType).Value = ... 形式 c) 始终使用 SQL 参数,包括 WHERE 子句目标 d) ExecuteNonQuery 会比 DataReader 更好
  • @DeanOC 不幸出现了这个错误,A generic error... 详细信息。

标签: vb.net gdi+


【解决方案1】:

我试试这个并工作。

Dim mstream As New System.IO.MemoryStream()
                'PictureBox1.Image = New Bitmap(Image.FromStream(mstream))
                PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
                Dim arrImage() As Byte = mstream.GetBuffer()

【讨论】:

  • 不要使用GetBuffer(),使用ToArray() 缺点在MSDN上有明确的描述...and here
猜你喜欢
  • 2010-12-12
  • 1970-01-01
  • 2019-01-02
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多