Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Saving Picturebox's image into MSSQL Server 2005 Image datatype

Posted on 2006-06-29
4
Medium Priority
?
508 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 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