Solved

Saving Picturebox's image into MSSQL Server 2005 Image datatype

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

19 Experts available now in Live!

Get 1:1 Help Now