Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Saving Picturebox's image into MSSQL Server 2005 Image datatype

Posted on 2006-06-29
4
Medium Priority
?
511 Views
Last Modified: 2011-09-20
Hi Experts,

Newbie here. Can someone guide me how to insert an image of a picturebox into a MSSQL Server of datatype(Image)?

I have a picturebox named "pbxMap"
It's a JPEG Format
It's in pbxMap.image

I have a Image datatype column in the table named "Map"

-------------------------------------------------------------------------------------
I tried so many methods found in the Internet but maybe my knowledge is not there,
I couldn't make it....

Thank you so much :)
0
Comment
Question by:adriankohws
4 Comments
 
LVL 6

Expert Comment

by:manch
ID: 17007778
Declare a variable of Byte() type in Dot Net
and store it to the table

               Dim rowItem as DataRow

               'assign the row of the Table to the variable rowitem

               'Store the Picture Box Controls Picture (Buffer) to the Bytes Array
                Dim arrImage() As Byte = PictureBox.GetBuffer

               'Thus u can store the image to the Table (of Column Image Type)
                rowItem(ColumnName) = arrImage

I m not discussing hove the DataRow of the table will be fetched.

 
0
 
LVL 7

Accepted Solution

by:
pradeepsudharsan earned 375 total points
ID: 17007796
0
 
LVL 11

Expert Comment

by:LordWabbit
ID: 17007900
Here you are, and just for the sake of completeness I added the code to get it back out and into an image box (also needed it to test the code so...)
Of course you might be doing and insert instead of an update, change where needed.  

   '-=- Save image into database
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim oCon As New SqlConnection(ConnectionString)
        oCon.Open()
        Dim myCommand As New SqlCommand("UPDATE myTable SET MAP = @MAP WHERE ID = @ID", oCon)
        Dim myStream As New System.IO.MemoryStream
        pbxMap.Image.Save(myStream, System.Drawing.Imaging.ImageFormat.Jpeg)
        Dim byteMe() As Byte = myStream.ToArray()
        myStream.Close()
        myCommand.Parameters.AddWithValue("@MAP", byteMe)
        myCommand.Parameters.AddWithValue("@ID", 1)
        myCommand.ExecuteNonQuery()
        myCommand.Dispose()
        oCon.Close()
        oCon.Dispose()
    End Sub

    '-=- Get image out of the database
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim oCon As New SqlConnection(ConnectionString)
        oCon.Open()
        Dim myCommand As New SqlCommand("SELECT MAP FROM myTable WHERE ID = @ID", oCon)
        myCommand.Parameters.AddWithValue("@ID", 1)
        Dim myDataAdapater As New SqlDataAdapter(myCommand)
        Dim myDataTable As New DataTable
        myDataAdapater.Fill(myDataTable)
        Dim byteMe() As Byte = CType(myDataTable.Rows(0)("Map"), Byte())
        Dim myStream As New System.IO.MemoryStream(byteMe)
        pbxMap.Image = System.Drawing.Image.FromStream(myStream)
    End Sub
0
 
LVL 10

Author Comment

by:adriankohws
ID: 17015050
Oh, how can I accept the second assisted posting as an Assisted Answer?
Once I clicked on Accept, I can't find the buttons anymore....
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question