Retrieving picture from database
Hello again! So now i can save the picture in the database, now i have to retrieve it.
this is my code for the saving of the picture,
Dim ms As New MemoryStream()
studentpic.Image.Save(ms, studentpic.Image.RawFormat)
Dim arrImage() As Byte = ms.GetBuffer
ms.Close()
Dim strFilename As String = lblfilePath.Text.Substring(lblfilePath.Text.LastIndexOf("\"))
Dim cnn As New SqlConnection(connectionString:="data source=.\sqlexpress; integrated security=true; attachdbfilename=|datadirectory|\WAIS.mdf; user instance=true;")
Dim strSQL As String = "INSERT INTO ImagesStore (OriginalPath, ImageData)" & "VALUES (@OriginalPath, @ImageData)"
Dim cmd As New SqlCommand(strSQL, cnn)
With cmd
.Parameters.Add(New SqlParameter("@OriginalPath", SqlDbType.NVarChar, 50)).Value = strFilename
.Parameters.Add(New SqlParameter("@ImageData", SqlDbType.Image)).Value = arrImage
End With
cnn.Open()
cmd.ExecuteNonQuery()
cnn.Close()
i saw on the net that i have to the reverse of the pic, i have to convert byte into image. can you please help me. thank you
aishapot
Junior Poster in Training
75 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
I have the code for retrieving, but my problem is that, it's only retrieving one pic.
i have multiple pictures for multiple students. I have a Listview wherein the students is shown, and when you click on a student a new form will show where the information of the students is also shown. what i need to do is that, when i click a student their picture should also be displayed together with the information not just the first picture in the database. - was that clear? just tell me if it's not. :)
here's the code
Sub retrievepic()
Dim conn As New SqlConnection("data source=.\sqlexpress; integrated security=true; attachdbfilename=|datadirectory|\WAIS.mdf; user instance=true;")
Dim cmd As New SqlCommand("select ImageData from ImagesStore where ImageId=@ImageId", conn)
cmd.Parameters.AddWithValue("@ImageId", 3)
Try
conn.Open()
PictureBox1.Image = Image.FromStream(New IO.MemoryStream(CType(cmd.ExecuteScalar, Byte())))
Catch ex As Exception
MsgBox(ex.Message)
Finally
conn.Close()
End Try
End Sub
aishapot
Junior Poster in Training
75 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
aishapot
Junior Poster in Training
75 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
I've been so busy, I almost forgot about this post.
I'm going to try this NOW! I hope it will work! I'll let you know immediately!
aishapot
Junior Poster in Training
75 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
I tried saving the Field Image in the same table with the student info, it's saving but i've noticed that it saves two times, one it saves the picture without the image, the surname and other info is null, and then the second save would be without the picture.
what is the better solution? should i separate the image field with the student info?
i really need your brilliant minds. we only have til february to finish this. thank you
aishapot
Junior Poster in Training
75 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
Solved
Imports System.Data.SqlClient
Imports System.Data
Imports System.IO
Public Class Form1
Dim str As String = "Data Source=NET3\SQLEXPRESS;Initial Catalog=RestPos;Persist Security Info=True;User ID=sa;Password=password"
Dim con As New SqlClient.SqlConnection
'To open an image from computer
'-------------------------------
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
OpenFileDialog1.Title = "Please select a file"
OpenFileDialog1.InitialDirectory = "c:temp"
OpenFileDialog1.ShowDialog()
TextBox2.Text = OpenFileDialog1.FileName.ToString '--->To Show the file path in textbox2
PictureBox1.ImageLocation = TextBox2.Text '--->To show selected image in picturebox
End sub
'To insert selected image into database
'The datatype of the column in table to store image should be <image>
'--------------------------------------------------------------------
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
con.ConnectionString = str
Dim ms As New IO.MemoryStream()
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Dim arrimage() As Byte = ms.GetBuffer
Dim cmd As New SqlCommand("insert into image (Emp_Image)values(@picture)", con)
cmd.Parameters.Add(New SqlParameter("@Picture", SqlDbType.Image)).Value = arrimage
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
'We have successfully inserted the image into database.
'Now we want to Retrieve the image from database.
'-------------------------------------------------
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim stream As New IO.MemoryStream()
con.Open()
Dim command As New SqlCommand("select Emp_Image from Image where Emp_Id='" + TextBox3. Text + "'", con) '--->You can give Emp_id instead of Textbox value.
Dim image As Byte() = DirectCast(command.ExecuteScalar(), Byte())
stream.Write(image, 0, image.Length)
con.Close()
Dim bitmap As New Bitmap(stream)
PictureBox2.Image = bitmap '--->I have used another picturebox to display image from database.
End Sub
'Thats all, You can place a linklabel below the picturebox if you to change photo and update it in database.
There was an error under con.Open() it says The ConnectionString property has not been initialized.
aishapot
Junior Poster in Training
75 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
Nevermind, it's working properly now! :) I just removed the con.open & con.close under the retrieve code. Thank You so much prathapsv3!
aishapot
Junior Poster in Training
75 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0