简体   繁体   English

将图像从数据库加载到图片框

[英]Loading image from database to picturebox

I'm new to visual basic and I have a problem in loading the image from my database. 我是Visual Basic的新手,从数据库中加载图像时遇到问题。 I'm currently using image data type. 我目前正在使用图片数据类型。 This is how I save the image 这就是我保存图像的方式

Imports System.Data
Imports System.Data.SqlClient

Public Class ScannerX_Add
    Dim ImageFilename As String
    Dim ImageUpload As Image
    Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Scan.Click

        Try
            OpenFileDialog1.Title = "Please Select a File"
            OpenFileDialog1.InitialDirectory = "C:\Users\ITTestServer\Desktop\Dekstop\"
            OpenFileDialog1.ShowDialog()
            ScannerX_Pic.Image = Image.FromFile(OpenFileDialog1.FileName)
            ImageFilename = OpenFileDialog1.FileName
            ImageUpload = Image.FromFile(OpenFileDialog1.FileName)
        Catch ex As Exception
            MsgBox("Please insert scan finger")
            Exit Sub
        End Try
    End Sub



    Private Sub Btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Save.Click
        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 b() As Byte = ms.ToArray()
            Dim cmd As New SqlCommand("Insert into Scanner (Name,Finger) VALUES('" & TxtBox_Name.Text.Trim & "','" & imageNameTemp & "')", Connection)
            cmd.Parameters.Add("@BLOBData", SqlDbType.Image, b.Length).Value = b
            cmd.ExecuteNonQuery()
            MsgBox("Profile Has Been Saved")
            Me.Close()
        End If
    End Sub

Now I need to load my image to picturebox which is currently in the Main form. 现在,我需要将图像加载到当前处于“主”窗体中的图片picturebox

Private Sub ButtonX1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
        Command.CommandText = ("select Finger FROM Scanner")


        Command.Connection = Connection
        Dim da As New SqlDataAdapter(Command)
        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)
            ScannerX_Pic2.Image = Image.FromStream(stmBLOBData)
        End If
    End Sub

Now I get the error Object reference not set to an instance of an object . 现在我收到错误“ Object reference not set to an instance of an object

To save an image you could do something like this 要保存图像,您可以执行以下操作

Dim sql As String = "INSERT INTO Information VALUES(@name,@photo)"
            Image Img = PictureBox1.BackgroundImage
            Dim cmd As New SqlCommand(sql, con)
            cmd.Parameters.AddWithValue("@name", TextBox1.Text)
            Dim ms As New MemoryStream()
            Img.Save(ms, Img.RawFormat)
            Dim data As Byte() = ms.GetBuffer()
            Dim p As New SqlParameter("@photo", SqlDbType.Image)
            p.Value = data
            cmd.Parameters.Add(p)
            cmd.ExecuteNonQuery()

And to retrieve an image 并检索图像

cmd = New SqlCommand("select photo from Information where name='Rose'", con)
    Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
    If Not imageData Is Nothing Then
        Using ms As New MemoryStream(imageData, 0, imageData.Length)
            ms.Write(imageData, 0, imageData.Length)
            PictureBox1.BackgroundImage = Image.FromStream(ms, True)
        End Using

Also check out this article it's doing exactly what you are trying to achieve 同时查看这篇文章,它正在做您想要达到的目标

http://www.codeproject.com/Articles/437937/Save-and-Retrieve-Image-from-a-SQL-Server-Database http://www.codeproject.com/Articles/437937/Save-and-Retrieve-Image-from-a-SQL-Server-Database

The sql is selecting a column named "Finger": sql正在选择一个名为“ Finger”的列:

"select Finger FROM Scanner"

But it's later trying to build an Image object from a column named "imagedate": 但后来尝试从名为“ imagedate”的列构建Image对象:

ds.Tables(0).Rows(c - 1)("imagedate")

It needs to use the same column name as the SQL result set: 它需要使用与SQL结果集相同的列名:

ds.Tables(0).Rows(c - 1)("Finger")

There may be other problems as well, but that's what jumped out at me right away. 可能还存在其他问题,但这就是马上跳出来的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM