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
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