Results 1 to 14 of 14

Thread: [RESOLVED] [2005] Load Image From SQL Server to Image Control

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Resolved [RESOLVED] [2005] Load Image From SQL Server to Image Control

    Hey I am havin some trouble loading an image from my sql db into a image on my form....

    Using this code I got from the codebank:

    vb Code:
    1. Dim connection As New SqlConnection("blahblah")
    2.  
    3.         Dim command As New SqlCommand("SELECT ImageData FROM Style_Images WHERE ImageID = 1", connection)
    4.  
    5.         connection.Open()
    6.  
    7.         Dim pictureData As Byte() = DirectCast(command.ExecuteScalar(), Byte())
    8.  
    9.         connection.Close()
    10.         Dim picture As Image = Nothing
    11.  
    12.         Using stream As New IO.MemoryStream(pictureData)
    13.             picture = Image.FromStream(stream)
    14.         End Using

    When I try to assign picture to my Image control I get this message in the error list:

    Value of type 'System.Drawing.Image' cannot be converted to 'System.Web.UI.WebControls.Image'

    Am I completely off on the way to to do this?

  2. #2
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [2005] Load Image From SQL Server to Image Control

    may be you can try this with namespace

    Code:
           Dim picture As System.Drawing.Image = Nothing
            Dim stream As New IO.MemoryStream(pictureData)
            picture = System.Drawing.Image.FromStream(stream)
    __________________
    Rate the posts that helped you

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [2005] Load Image From SQL Server to Image Control

    yeah already importing System.Drawing

  4. #4
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [2005] Load Image From SQL Server to Image Control

    but did you tried with above code (with fully qualified name)?
    __________________
    Rate the posts that helped you

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [2005] Load Image From SQL Server to Image Control

    I think I described the problem wrong... basically confused on how to view the picture on the webform.

    When I do

    imgThumb = picture

    I get the above error.

  6. #6
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [2005] Load Image From SQL Server to Image Control

    can you show how imgThumb is declared?
    __________________
    Rate the posts that helped you

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [2005] Load Image From SQL Server to Image Control

    <asp:Image ID="imgThumb" runat="server" Height="168px" Width="144px" />

  8. #8
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [2005] Load Image From SQL Server to Image Control

    not sure if you would prefer saving image to disk and bind it to imgThumb??

    Code:
               Dim fs As New IO.FileStream(Server.MapPath("images\pic.jpeg"), FileMode.CreateNew)
            Dim bw As New IO.BinaryWriter(fs)
            bw.Write(pictureData )
            bw.Close()
            imgThumb.ImageUrl = "images\pic.jpeg"
    __________________
    Rate the posts that helped you

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [2005] Load Image From SQL Server to Image Control

    Yeah I already have them on disk if I wanted to go that way... but figured I would try to consolidate them into a table and work with them from there... If it can't be done through a table I will go back to using the .jpg's directly.

  10. #10
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [2005] Load Image From SQL Server to Image Control

    well in above code i am still using jpg file your getting from SQL in form of byte in pictureData
    __________________
    Rate the posts that helped you

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [2005] Load Image From SQL Server to Image Control

    getting FileMode is not declared. am I missing a namespace?

  12. #12
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [2005] Load Image From SQL Server to Image Control

    it should be IO.FileMode.CreateNew
    __________________
    Rate the posts that helped you

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [2005] Load Image From SQL Server to Image Control

    Cool that works... thanks alot dude...

    You must spread some Reputation around before giving it to riteshjain1982 again.

  14. #14
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [RESOLVED] [2005] Load Image From SQL Server to Image Control

    Glad it worked

    buddy you need to rate atleast 10 people before you can rate me
    __________________
    Rate the posts that helped you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width