使用byte将图像插入datagridview [英] Insert image into datagridview using byte

查看:128
本文介绍了使用byte将图像插入datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在将数据图像插入数据网格视图时遇到问题;



首先是表格类:



 Dim imgbyte As Byte()= Nothing 





表单加载:



 table.Columns.Add(Image,GetType(Byte())) 





按钮生成数据到datagridview;



 table.Rows.Add(GetType(Byte)





Button2将图像作为字节添加到picturebox1;



如果OpenFileDialog1.ShowDialog = vbOK那么

结束如果

Dim myimage As Image = Image.FromFile(OpenFileDialog1.FileName)

Dim imagestream As System.IO.MemoryStream = New System.IO.MemoryStream

myimage.Save(imagestream,System.Drawing.Imaging .ImageFormat.Jpeg)
imgbyte = imagestream.GetBuffer

PictureBox1.Image = Drawing.Image.FromStream(imagestre am)









然后我收到以下错误:



 System.Data.dll中出现未处理的System.ArgumentException类型异常

附加信息:类型值与列类型不匹配不能存储< System.Byte>在图像列中。预期的类型是Byte []。





标记以下行;



 table.Rows.Add(GetType(字节)





任何人都可以提供建议吗?



我尝试过:



使用搜索按钮研究论坛。

解决方案

  Imports  System.IO 
Imports System.Drawing.Imaging
Imports System.Drawing

公开 Form1
Dim ms As MemoryStream
Dim imgData() As 字节
Dim sqlSTR As 字符串

' 此代码以字节的形式保存图像进入SQl服务器

Sub saveImage()
Dim p 作为 SqlParameter
尝试
dim sqlSTR As String = UPDATE tableName SET imageField = @imageParameter
使用 sqlCmmnd As SqlClient.SqlCommand(sqlSTR,con)
如果 PictureBox1.Image N othing 然后
ms = MemoryStream
PictureBox1.Image。保存(ms,ImageFormat.Jpeg)
imgData = 字节(ms.Length){ }
ms.Position = 0
ms。读取(imgData, 0 , imgData.Length)
sqlCmmnd.Parameters.AddWithValue( @ logo,imgData)
其他
使用 p
.ParameterName = @ imageParameter
.DbType = DbType.Binary
.Value = System.DBNull.V alue
结束 使用
sqlCmmnd.Parameters.Add(p)
结束 如果
con.Open()
sqlCmmnd.ExecuteNonQuery()
结束 使用
con.Close()
Catch ex As 异常
MsgBox(ex.Message.ToString,MsgBoxStyle.Exclamation, 错误
结束 尝试
结束 Sub


' 此子获取字节ima ge并将其转换然后显示在图片框上,您也可以将其添加到datagriedview

Sub loadImage()
尝试
Dim sqlDA As SqlDataAdapter( SELECT imageField FROM tableName WHERE uniqueField = '&替换(TextBox1.Text.Trim, ' '')& ',。con)
Dim sqlCB 作为 SqlCommandBuilder(sqlDA)
sqlDT.Reset()
sqlDA.Fill(sqlDT)
如果 IsDBNull(sqlDT.Rows( 0 )( imageField))然后
imgData = sqlDT.Rows( 0 )( imageField
Dim ms As MemoryStream = MemoryStream(imgData)
PictureBox1.Image = Image.FromStream(ms)
' DataGridView1.Rows(index).Add(Image.FromStream(ms))
Else
PictureBox1.Image = Nothing
End 如果
Catch ex As 异常
MsgBox(ex.Message.ToString,MsgBoxStyle.Exclamation, 错误
结束 尝试

结束 Sub
结束


Hi,

I'm having a problem inserting a byte image into my datagridview;

Firstly Form Class:

Dim imgbyte As Byte() = Nothing



Form Load:

table.Columns.Add("Image", GetType(Byte()))



Button to generate data to datagridview;

table.Rows.Add(GetType(Byte)



Button2 to add image to picturebox1 as byte;

If OpenFileDialog1.ShowDialog = vbOK Then

        End If

        Dim myimage As Image = Image.FromFile(OpenFileDialog1.FileName)

        Dim imagestream As System.IO.MemoryStream = New System.IO.MemoryStream

        myimage.Save(imagestream, System.Drawing.Imaging.ImageFormat.Jpeg)
        imgbyte = imagestream.GetBuffer

        PictureBox1.Image = Drawing.Image.FromStream(imagestream)





I then get the following error:

An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll

Additional information: Type of value has a mismatch with column typeCouldn't store <System.Byte> in Image Column.  Expected type is Byte[].



Which flags the following line;

table.Rows.Add(GetType(Byte)



Anyone able to advise?

What I have tried:

Researching forums using search button.

解决方案

Imports System.IO
Imports System.Drawing.Imaging
Imports System.Drawing

Public Class Form1
    Dim ms As MemoryStream
    Dim imgData() As Byte
    Dim sqlSTR As String

    'This code saves the image in form of byte into SQl Server

    Sub saveImage()
        Dim p As New SqlParameter
        Try
          dim  sqlSTR As String= "UPDATE tableName SET imageField = @imageParameter"
            Using sqlCmmnd As New SqlClient.SqlCommand(sqlSTR, con)
                If Not PictureBox1.Image Is Nothing Then
                    ms = New MemoryStream
                    PictureBox1.Image.Save(ms, ImageFormat.Jpeg)
                    imgData = New Byte(ms.Length) {}
                    ms.Position = 0
                    ms.Read(imgData, 0, imgData.Length)
                    sqlCmmnd.Parameters.AddWithValue("@logo", imgData)
                Else
                    With p
                        .ParameterName = "@imageParameter"
                        .DbType = DbType.Binary
                        .Value = System.DBNull.Value
                    End With
                    sqlCmmnd.Parameters.Add(p)
                End If
                con.Open()
                sqlCmmnd.ExecuteNonQuery()
            End Using
            con.Close()           
        Catch ex As Exception
            MsgBox(ex.Message.ToString, MsgBoxStyle.Exclamation, "Error")
        End Try
    End Sub


   'This sub gets the byte image and converts it then displays on the picturebox, you can also add it to datagriedview

   Sub loadImage()
        Try
            Dim sqlDA As New SqlDataAdapter("SELECT imageField FROM tableName WHERE uniqueField ='" & Replace(TextBox1.Text.Trim, "'", "''") & "'",.con)
            Dim sqlCB As New SqlCommandBuilder(sqlDA)
            sqlDT.Reset()
            sqlDA.Fill(sqlDT)
            If Not IsDBNull(sqlDT.Rows(0)("imageField ")) Then
                imgData = sqlDT.Rows(0)("imageField ")
                Dim ms As MemoryStream = New MemoryStream(imgData)
                PictureBox1.Image = Image.FromStream(ms)
                ' for DataGridview DataGridView1.Rows(index).Add(Image.FromStream(ms))
            Else
                PictureBox1.Image = Nothing
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString, MsgBoxStyle.Exclamation, "Error")
        End Try

    End Sub
End Class


这篇关于使用byte将图像插入datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆