Ordiz
Ordiz

Reputation: 49

Loading image from database to picturebox

I'm new to visual basic and I have a problem in loading the image from my database. 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.

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.

Upvotes: 0

Views: 546

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

The sql is selecting a column named "Finger":

"select Finger FROM Scanner"

But it's later trying to build an Image object from a column named "imagedate":

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

It needs to use the same column name as the SQL result set:

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

There may be other problems as well, but that's what jumped out at me right away.

Upvotes: 0

Khaled El Kholy
Khaled El Kholy

Reputation: 136

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

Upvotes: 3

Related Questions