Solved

Saving Picturebox's image into MSSQL Server 2005 Image datatype

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now