我们如何以二进制格式将图像插入数据库 [英] how we can insert the image in database in binary format

查看:79
本文介绍了我们如何以二进制格式将图像插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否提出建议,如何将图片框中的图片插入二进制文件中?

can u give the suggestion how can i insert image from picturebox in binary

推荐答案

Try:
Try:
Dim ms As New MemoryStream()
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Return ms.ToArray()

将图像转换为字节数组.
并且:

That converts an Image to an array of bytes.
And:

Using con As New SqlConnection(strConnect)
	con.Open()
	Using com As New SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con)
		com.Parameters.AddWithValue("@C1", myValueForColumn1)
		com.Parameters.AddWithValue("@C2", myValueForColumn2)
		com.ExecuteNonQuery()
	End Using
End Using

将项目插入数据库.

您要做的就是将两者结合起来,并进行修改以适合您的方案.

That inserts items to a DB.

All you have to do is combine the two, and modify to fit your scheme.


用于浏览,保存和检索图像的代码

Code for browse-save-retrieve image

Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
       Dim a As New OpenFileDialog
       a.Filter = "Image(*.jpg,*.png,*.bmp,*.JPeg,*.Gif)|*.jpg;*.png;*.bmp;*.JPeg;*.Gif;"
       a.Title = "Browse icon for ..."
       If a.ShowDialog = DialogResult.OK Then
           Dim img As Bitmap = Image.FromFile(a.FileName)
           img.SetResolution(72, 72)
           PictureBox1.BackgroundImage = img
           PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
           IsChanged = True
       End If
   End Sub







'''read image from picturebox an Save image in sql in binary format
'note  IsChanged is boolean variable, declare on page level, set it true when image is changed. do not update image when there is no change in it.
        If IsChanged = True Then
            IsChanged = False
            Dim stream1 As New IO.MemoryStream
            Dim img As Bitmap
            img = PictureBox1.BackgroundImage
            Using img
                Using stream As New IO.MemoryStream
                    Try
                        img.Save(stream, Imaging.ImageFormat.Bmp)
                    Catch ex As Exception
                        Try
                            img.Save(stream, Imaging.ImageFormat.Png)
                        Catch ex1 As Exception
                            Try
                                img.Save(stream, Imaging.ImageFormat.Gif)
                            Catch ex2 As Exception
                                Try
                                    img.Save(stream, Imaging.ImageFormat.MemoryBmp)
                                Catch ex3 As Exception

                                End Try
                            End Try

                        End Try
                    End Try
                    stream1 = stream
                    stream.Dispose()
                End Using
            End Using
            Try
                Cmd.CommandText = "update Dtl set Img=@Img"
                Cmd.Parameters.Clear()
                Cmd.Parameters.AddWithValue("@Img", stream1.GetBuffer)
                Cmd.ExecuteNonQuery()
                Cmd.Parameters.Clear()
                stream1.Dispose()
            Catch ex As Exception
            End Try





'''fatch image from database & display in picture box
            Try
                Dim dt As New DataTable
                dt = DBGetData("select * from Dtl")
                For i = 0 To dt.Rows.Count - 1
                    txtNm.Text = dt.Rows(i)("Nm")
                    txtAdd.Text = dt.Rows(i)("Add")
                    txtWebsite.Text = dt.Rows(i)("Website")
                    txtMailId.Text = dt.Rows(i)("MailId")
                    txtCont1.Text = dt.Rows(i)("ContNo1")
                    txtCont2.Text = dt.Rows(i)("ContNo2")

                    Dim ImgData As Byte() = DirectCast(dt.Rows(i)("Img"), Byte())

                    Dim stream As New IO.MemoryStream(ImgData)
                    Using stream
                        Img = Image.FromStream(stream)
                    End Using
                    stream.Dispose()
                    PictureBox1.BackgroundImage = Img
                    PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
                Next
            Catch ex As Exception
            End Try



祝您编码愉快!
:)



Happy Coding!
:)


这篇关于我们如何以二进制格式将图像插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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