PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET How can I save the SQL image from PictureBox

Reply

How can I save the SQL image from PictureBox

 
Thread Tools Rate Thread
Old 05-08-2007, 09:10 PM   #1
kenken
Guest
 
Posts: n/a
Default How can I save the SQL image from PictureBox


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


  Reply With Quote
Old 05-08-2007, 09:39 PM   #2
Michael Phillips, Jr.
Guest
 
Posts: n/a
Default Re: How can I save the SQL image from PictureBox

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



"kenken" <kenken@kenken.com> wrote in message
news:Om%23%237y51HHA.3940@TK2MSFTNGP05.phx.gbl...
>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
>



  Reply With Quote
Old 05-08-2007, 09:52 PM   #3
kenken
Guest
 
Posts: n/a
Default Re: How can I save the SQL image from PictureBox

thx, I guess moethod 2 is better for my project.
I'll search some soultions!

"Michael Phillips, Jr." <mphillips53@nospam.jun0.c0m> wrote in message
news:OE9$ED61HHA.4004@TK2MSFTNGP05.phx.gbl...
>> 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.
>
>
>
> "kenken" <kenken@kenken.com> wrote in message
> news:Om%23%237y51HHA.3940@TK2MSFTNGP05.phx.gbl...
>>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
>>

>
>



  Reply With Quote
Old 05-08-2007, 10:37 PM   #4
Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
Default Re: How can I save the SQL image from PictureBox

"kenken" <kenken@kenken.com> schrieb:
>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>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

  Reply With Quote
Old 06-08-2007, 05:31 AM   #5
Cor Ligthert[MVP]
Guest
 
Posts: n/a
Default Re: How can I save the SQL image from PictureBox

Kenken,

Here a sample of all action, all not needed stuff which you see in other
links is removed.

http://www.vb-tips.com/DataSetImage.aspx

Cor


"kenken" <kenken@kenken.com> schreef in bericht
news:Om%23%237y51HHA.3940@TK2MSFTNGP05.phx.gbl...
>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
>


  Reply With Quote
Old 06-08-2007, 07:12 PM   #6
kenken
Guest
 
Posts: n/a
Default Re: How can I save the SQL image from PictureBox

thx you guys, the attached links really help!


"Cor Ligthert[MVP]" <notmyfirstname@planet.nl> wrote in message
news:785F64A0-B584-49D2-948E-9EB9A13346B6@microsoft.com...
> Kenken,
>
> Here a sample of all action, all not needed stuff which you see in other
> links is removed.
>
> http://www.vb-tips.com/DataSetImage.aspx
>
> Cor
>
>
> "kenken" <kenken@kenken.com> schreef in bericht
> news:Om%23%237y51HHA.3940@TK2MSFTNGP05.phx.gbl...
>>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
>>

>



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off