Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
870 views
in Technique[技术] by (71.8m points)

vb.net - How to add image from database to 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()

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.

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

1- datatype should be image in database

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...