Solved

Saving Picturebox's image into MSSQL Server 2005 Image datatype

Posted on 2006-06-29
4
487 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 125 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

713 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