[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:
Dim connection As New SqlConnection("blahblah")
Dim command As New SqlCommand("SELECT ImageData FROM Style_Images WHERE ImageID = 1", connection)
connection.Open()
Dim pictureData As Byte() = DirectCast(command.ExecuteScalar(), Byte())
connection.Close()
Dim picture As Image = Nothing
Using stream As New IO.MemoryStream(pictureData)
picture = Image.FromStream(stream)
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?
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)
Re: [2005] Load Image From SQL Server to Image Control
yeah already importing System.Drawing
Re: [2005] Load Image From SQL Server to Image Control
but did you tried with above code (with fully qualified name)?
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.
Re: [2005] Load Image From SQL Server to Image Control
can you show how imgThumb is declared?
Re: [2005] Load Image From SQL Server to Image Control
<asp:Image ID="imgThumb" runat="server" Height="168px" Width="144px" />
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"
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.
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
Re: [2005] Load Image From SQL Server to Image Control
getting FileMode is not declared. am I missing a namespace?
Re: [2005] Load Image From SQL Server to Image Control
it should be IO.FileMode.CreateNew
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.
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 :D