尝试从流中创建图像时出现ArgumentException

时间:2018-08-07 10:16:22

标签: vb.net image

在我更新datagridview中的数据之后,并且在我单击datagridview中的更新数据的行之后,会出现错误

enter image description here

Private Sub DataGridView2_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellClick
    Dim i As Integer

    i = DataGridView2.CurrentRow.Index

    Me.txtEmployeeID.Text = DataGridView2.Item(0, i).Value
    Me.txtFirstName.Text = DataGridView2.Item(1, i).Value
    Me.txtMiddleName.Text = DataGridView2.Item(2, i).Value
    Me.txtLastName.Text = DataGridView2.Item(3, i).Value
    Me.txtGender.Text = DataGridView2.Item(4, i).Value
    Me.txtContactNumber.Text = DataGridView2.Item(5, i).Value
    Me.txtupAge.Text = DataGridView2.Item(6, i).Value
    Me.txtupAddress.Text = DataGridView2.Item(7, i).Value
    Me.Bdate.Text = DataGridView2.Item(8, i).Value
    Me.txtPos.Text = DataGridView2.Item(9, i).Value

    If DataGridView2.Item(10, i).Value Is Nothing Then Return
    Using m As MemoryStream = New MemoryStream(CType(DataGridView2.Item(10, i).Value, Byte()))
       PictureBox2.Image = CType(Image.FromStream(m).Clone(), Image)
    End Using
End Sub

**编辑**

即时更新时,我数据库中的图像类型将更改为0x,即没有图像被保存。

这是我的更新代码

   cn.Close()
    cn.Open()
    With cmd
        .Connection = cn
        .CommandText = ("Update TBL_EMPLOYEE SET FirstName= '" & txtFirstName.Text & "', MiddleName='" & txtMiddleName.Text & "',LastName='" & txtLastName.Text & "', Gender='" & txtGender.Text & "',Age='" & txtupAge.Text & "' ,Address='" & txtupAddress.Text & "', Position='" & txtPos.Text & "',BirthDate='" & Bdate.Value.Date & "', [Picture]=@PID where [EmployeeID]=@EID ")
        .Parameters.AddWithValue("@EID", CInt(txtEmployeeID.Text))
        .Parameters.Add("PID", SqlDbType.Image).Value = ms.ToArray()
        .ExecuteNonQuery()
        .Dispose()
        .Parameters.Clear()
        txtFirstName.Text = ""
        txtMiddleName.Text = ""
        txtLastName.Text = ""
        txtGender.Text = ""
        txtContactNumber.Text = ""
        txtupAge.Text = ""
        txtupAddress.Text = ""
        txtPos.Text = ""
        Bdate.Text = ""
        PictureBox2.Image = Nothing


        MsgBox("Employee Updated", vbInformation, "Information Message")
        datagridshow()

    End With
End If

enter image description here

当我更新此图像时,该图像将不会保存。我的更新中语法错误

enter image description here

2 个答案:

答案 0 :(得分:1)

我缺少此代码:PictureBox2.Image.Save(ms, PictureBox2.Image.RawFormat)我的错。 :'(

答案 1 :(得分:0)

如果您确定DataGridView2.Item(10, i).Value在此处具有值,则可能是MemoryStream的处理速度太快了。我建议尝试:

m As MemoryStream = New MemoryStream(CType(DataGridView2.Item(10, i).Value, Byte()))
PictureBox2.Image = Image.FromStream(m)

没有Using