Load image column from database into image control

Hello all,

I am using VB.NET 2005, and I have an image from database that need to load into image control in the form. This is what I got so far.

1) strSQL = "SELECT MyImage FROM ImageTable"

2) making connection to database

3) sending sql command to retrieve image from database

cmd = new SqlCommand(strSQL,myconnection)

4) myReader = cmd.ExecuteReader()

5) myReader.Read()

6) Load image from DataReader into Image control

'This is the step I don't know how to do it. Please help

Thanks very much.




Answer this question

Load image column from database into image control

  • WBAS

    youre solution is exactly what i need! i can save images to the database in SQL and i can easily display them in a picturebox but i cant seem to figure out how to display them in image control. i only know VB and i was hoping you would be able to fill in the blanks for me ! or point me in the right direction.

    this is what i have so far:

    Dim PictureCol As Integer = 0

    Dim cn As New SqlConnection("Server=Seakwil;Database=Master;Trusted_Connection=yes")

    Dim cmd As New SqlCommand("SELECT Photo FROM TMP_Pic", cn)

    cn.Open()

    Dim dr As SqlDataReader = cmd.ExecuteReader()

    dr.Read()

    Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte

    dr.GetBytes(PictureCol, 0, b, 0, b.Length)

    dr.Close()

    cn.Close()

    Dim ms As New System.IO.MemoryStream(b)

    imgBLOB.ImageUrl = System.Drawing.Image.FromStream(ms)

    - and the compiler says " Value of type 'System.Image.FromStream(ms)' cannot be converted to 'String' "

    what could i do to get the picture in the Image control!!!

    Nathan


  • m_umair_85

    Thank you Val and Dman1 for your replied. I have marked your replied as an answered.

    Cheers,



  • RasmusChristensen

  • Guardian-ND

    I believe one of the samples using DataReader to read the data.

  • samsquared

    Look at Load image from Database, I posted the code for this.

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=250564&SiteID=1

    This code shows storing and showing.

    Walter


  • toben888

    Manipulated from MS Sample:

    http://support.microsoft.com/kb/316887/en-us

    Private Sub SqlBlob2File(ByVal DestFilePath As String)

    Dim PictureCol As Integer = 0 ' the column # of the BLOB field

    Dim cn As New SqlConnection("server=localhost;integrated security=yes;database=NorthWind")

    Dim cmd As New SqlCommand("SELECT Picture FROM Categories WHERE CategoryName='Test'", cn)

    cn.Open()

    Dim dr As SqlDataReader = cmd.ExecuteReader()

    dr.Read()

    Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte

    dr.GetBytes(PictureCol, 0, b, 0, b.Length)

    dr.Close()

    cn.Close()

    Dim ms As New System.IO.MemoryStream(b)

    Me.PictureBox1.Image = System.Drawing.Image.FromStream(ms)

    End Sub



  • Sarah71

    thank you very much for the links, but I was wondering if anyone has code sample loading image from DataReader instead of DataSet.

    Thanks again.



  • elsamma

    I was trying to solve the same problem, could not get to solve the problem
    I bought the Pro ASP.NET 2.0 book from apress and "implemented" the solution to this(page 983)
    this did not work for me as i'm not using a <img> tag but a <asp:Image> control.

    My solution was this:
    Add a <asp:Image ID="img" runat="server"...> control to the page; then on Page_PreRender i did:

    //Story is a class from the database
    img.Visible = ((int)Story.Picture > -1);
    if (img.Visible)
    {
      img.Attributes["onload"] = string.Format(this.src='ViewImage.aspx img={0}';"
    , Story.Picture);
    }

    The clue in the solution is the Attributes property, the asp:Image class renders the properies of the html img tag, adding entries to Attibutes[<AtributName>] will give you access to all the tags attributes and only the properies given to us by the asp control.

    or assign it to the image url by doing:
    //Story is a class from the database
    img.Visible = ((int)Story.Picture > -1);
    if (img.Visible)
    {
    img.ImageUrl = string.Format("ViewImage.aspx img={0}"
    , Story.Picture);
    }

    if your page does not need asynchronous loading.

    I hope that this was helpful 

    Walter Verhoeven


      


  • SiW

    I am trying to load an image saved in Access to a picturebox in VB.NET. I tried the first link, which seems like it should work perfectly, but I am getting an error.

    Dim bytBLOBData() As Byte = row.CK_Signature
    Dim stmBLOBData As New MemoryStream(bytBLOBData)
    Me.imgAPSignature.Image = Image.FromStream(stmBLOBData)

    on the last line, I am getting the following error:
    System.ArgumentException: Invalid parameter used.
    at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement)
    at System.Drawing.Image.FromStream(Stream stream)

    Can anyone help What is wrong with my MemoryStream



  • Load image column from database into image control