How can I save the SQL image from PictureBox

K

kenken

I use below 2 sub to save and load the image file into MS SQL Sever 2000,
and display it on a PictureBox control.
For now I wanna to save the image file (shown on the PictureBox Contorl)
into local PC,
I have search some online materials but found no help, would anyone give me
some hints!
thx alot!!



Private Sub saveImage()
Try
Dim cn As New SqlConnection(modPublic.sConnectionString)
Dim iSQL As String
iSQL = "INSERT INTO TABLE (id, ProductImage) VALUES (id,
@Photo)"

Dim cmd As New SqlCommand(iSQL, cn)
Dim strPhotoFilePath As String = Trim(Me.txtUpload.Text)
Dim fsPhotoFile As New FileStream(strPhotoFilePath,
FileMode.Open, FileAccess.Read)
Dim bytPhotoData(fsPhotoFile.Length() - 1) As Byte
Try
fsPhotoFile.Read(bytPhotoData, 0, bytPhotoData.Length)
fsPhotoFile.Close()
Dim prm As New SqlParameter("@Photo", SqlDbType.VarBinary, _
bytPhotoData.Length, ParameterDirection.Input, False, _
0, 0, Nothing, DataRowVersion.Current, bytPhotoData)
cmd.Parameters.Add(prm)
cn.Open()
cmd.ExecuteNonQuery()
Catch exc As Exception
MessageBox.Show(exc.Message, strAppTitle)
Finally
cn.Close()
End Try
Catch exc As Exception
MessageBox.Show(exc.Message, strAppTitle)
End Try
End Sub



Private Sub loadImage()
Dim cn As New SqlConnection(modPublic.sConnectionString)
Dim cmd As New SqlCommand("SELECT * FROM TABLE WHERE id = @id", cn)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
Try
da.Fill(ds, "PhotoImage")
Dim c As Integer = ds.Tables("PhotoImage").Rows.Count
If c > 0 Then
Dim bytPhotoData() As Byte =
ds.Tables("PhotoImage").Rows(c - 1)("ProductImage")
Dim stmPhotoData As New MemoryStream(bytPhotoData)
picENQImage.Image = Image.FromStream(stmPhotoData)
End If
Catch exc As Exception
MessageBox.Show("Error in loading Image.", strAppTitle)
End Try
End Sub
 
M

Michael Phillips, Jr.

Dim stmPhotoData As New MemoryStream(bytPhotoData)
picENQImage.Image = Image.FromStream(stmPhotoData)

You have two choices:
1) Use the System.Drawing.Image method Save to save as a file.
Choose the image format to save it to(e.g., bitmap, png, gif, etc.)
2) Use a FileStream to write the memory stream to a file.
The memory stream is already properly formatted as an image.
There is no need to decode and encode it unless you wish to save it
as a different image format.
 
H

Herfried K. Wagner [MVP]

kenken said:
I use below 2 sub to save and load the image file into MS SQL Sever 2000,
and display it on a PictureBox control.

How To Read and Write BLOB Data by Using ADO.NET with Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;308042>

HOW TO: Read and Write a File to and from a BLOB Column by Using ADO.NET and
Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;316887>

HOW TO: Read and Write a File to and from a BLOB Column by Using Chunking in
ADO.NET and Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;317034>

HOW TO: Read and Write BLOB Data by Using ADO.NET Through ASP.NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;326502>

..NET Framework Developer's Guide -- Writing BLOB Values to a Database
<URL:http://msdn.microsoft.com/library/en-us/cpguide/html/cpconwritingblobvaluestodatabase.asp>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top