i got a question about the upload a pic and save it to database
can anyone show me how to do the process of upload a picture to store it to the database. and when i need to use the picture i can retrieve from database ? can give me an example of it ? thank you ~~
'To load a picture from file to a picture box.
Me.PictureBox1.Image=Image.FromFile("c:\fileName.gif")
'To save picture to database
'in my own case, I used a typed-dataset and
'a Microsoft SQL Server 2000 as the datastore.
'in the dataset the column for the photograph is
'of type 'base64Binary' and of type 'image' in the database.
Dim bytPhoto() As Byte
Dim memStream As New IO.MemoryStream()
Me.PictureBox1.Image.Save(memStream, Drawing.Imaging.ImageFormat.Gif)
'you can specify image format of choice
bytPhoto = memStream.ToArray()
'you can assign the variable 'bytPhoto' to the appropriate
'column of the dataset datarow
'in my case
dstEmpData.Employees.Photograph = bytPhoto
'where dstEmpData is the dataset,
'Employees and Photograph represents the
'datatable and datarow respectively.
'you can update the database as usual.
'To display the photograph from the
'database in a picture box try this:
Dim memStream As New IO.MemoryStream()
Dim bytPhoto() As Byte
here is my coding to save a picture because i didnt use the dataset to store my information so how can i do ?
==============================================
this is open and select image
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Reading a picture and put it in a bytearray
If fo.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(fo.FileName, _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
abyt = br.ReadBytes(CInt(fs.Length))
br.Close()
'just to show the sample without a fileread error
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End If
End Sub
=============================================
after that when i click the save button
Me.PictureBox1.Image.Save(memStream, Drawing.Imaging.ImageFormat.Gif)
'you can specify image format of choice
bytPhoto = memStream.ToArray()
if you check you insert statement, the photograph 'bytPhoto' is within single quotes. Single quotes should only be used for strings. Remove the quotes and try again.
If your code fragment below, I have highlighted the bytPhoto segment.