如何将数据库中的图像添加到PictureBox? [英] How to add image from database to PictureBox?

查看:106
本文介绍了如何将数据库中的图像添加到PictureBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用它来从数据库中获取图像字节

I am using this to get image bytes from the database

cmd.CommandText = "select imagedate from projectimages where imagename = '" + _
    ListBox1.Text + "' and CSVprojectref=checksum('" + textboxFileRef.Text + "')"

Dim img As Object = cmd.ExecuteScalar()

现在我该如何将它添加到PictureBox.image。我在检索图像并在PictureBox中显示它时遇到了很多麻烦。

Now how can I add this to PictureBox.image. I am having a lot of trouble retrieving the image and displaying it in the PictureBox.

数据类型是sql数据库中的Image,我使用此代码将图像保存到db

The datatype is Image in sql database and i use this code to save image to db

         Dim ms As New IO.MemoryStream

        If imageFilename.Contains("jpeg") Or imageFilename.Contains("jpg") Then
            imageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)

        End If
        If imageFilename.Contains("png") Then
            imageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
        End If
        If imageFilename.Contains("gif") Then
            imageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)
        End If
        If imageFilename.Contains("bmp") Then
            imageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
        End If

        Dim bytes() As Byte = ms.ToArray
        Dim img As String = Convert.ToBase64String(bytes)


        Dim cmd As New OleDb.OleDbCommand("insert projectimages values('" + imageNameTemp + "','" + img + "',CHECKSUM('" + textboxFileRef.Text + "'))", con)
        cmd.ExecuteNonQuery()


推荐答案

经过5-6个小时的搜索论坛和博客以及我喜欢的一切...将图像保存到数据库

After 5-6 hours of searching forums and blogs and everything i fond this... to save image to database

1-数据类型应该是数据库中的图像

1- datatype should be image in database

现在将图像存储到sql数据库时添加此代码

Now add this code when storing image to the sql database

    OpenFileDialog1.ShowDialog()
    imageFilename = OpenFileDialog1.FileName
    Dim imageUpload As Image
    imageUpload = Image.FromFile(OpenFileDialog1.FileName)



    If imageFilename <> "" Then

        Dim imageNameTemp As String

        imageNameTemp = imageFilename

        While (imageNameTemp.Contains("\"))


            imageNameTemp = imageNameTemp.Remove(0, imageNameTemp.IndexOf("\") + 1)
        End While

        Dim ms As New IO.MemoryStream

        If imageFilename.Contains("jpeg") Or imageFilename.Contains("jpg") Then
            imageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)

        End If
        If imageFilename.Contains("png") Then
            imageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
        End If
        If imageFilename.Contains("gif") Then
            imageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)
        End If
        If imageFilename.Contains("bmp") Then
            imageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
        End If

        'Dim cmd As New SqlCommand("INSERT INTO projectimages (imagename,imagedate,csvprojectref) VALUES ('" + imageFilename + "',@BLOBData,CHECKSUM('" + textboxFileRef.Text + "'))", con)

        Dim b() As Byte = ms.ToArray()

        Dim cmd As New SqlCommand("INSERT INTO projectimages (imagename,imagedate,csvprojectref) VALUES ('" + imageNameTemp + "',@BLOBData,CHECKSUM('" + textboxFileRef.Text + "'))", con)

        cmd.Parameters.Add("@BLOBData", SqlDbType.Image, b.Length).Value = b
        '    Dim cmd As New SqlCommand("insert projectimages(imagename,imagedate,csvprojectref) values('imagma','" + img + "',CHECKSUM('" + textboxFileRef.Text + "'))", con)

        cmd.ExecuteNonQuery()

        '  cmdTemp.Parameters.Add("@photo", SqlDbType.Image, b.Length).Value = b

    End If

何时检索要插入图片框的数据使用此代码......

And when to retrieve data to insert into picture box use this code...

  cmd.CommandText = "select imagedate from projectimages where imagename = '" +      ListBox1.Text + "' and CSVprojectref=checksum('" + textboxFileRef.Text + "')"


        cmd.Connection = con
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds, "projectimages")
        Dim c As Integer = ds.Tables(0).Rows.Count
        If c > 0 Then
            Dim bytBLOBData() As Byte = _
                ds.Tables(0).Rows(c - 1)("imagedate")
            Dim stmBLOBData As New MemoryStream(bytBLOBData)
            PictureBox1.Image = Image.FromStream(stmBLOBData)
        End If

这篇关于如何将数据库中的图像添加到PictureBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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