How to display an image from sql server DB in an ASP.NET image webcontrol

Hello! I know how to display using response.binary, but I need the image in a gif format to make it readable in a asp.net page. Thnaks. Robossa
robossaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

robossaAuthor Commented:
I have a search file  for horses where I click a Imagebutton.url="index.aspx?para1=" + horseid

THEN

In the index.aspx file I have the code to read the binary field from database, but in an specific image tag . There is where I have the problem.
0
billsonCommented:
This sample should work :

Dim SQL
SQL = "Server=SQLServerName; Database=TestDB; uid=tester; pwd=1234;"
Dim Conn As New System.Data.SqlClient.SqlConnection(SQL)
Conn.Open()

SQL = "SELECT TOP 1 image_file FROM ImageTable WHERE horseid = " & Request.QueryString("para1")
Dim command As New System.Data.SqlClient.SqlCommand(SQL, Conn)
Dim reader As System.Data.SqlClient.SqlDataReader
reader = command.ExecuteReader()

If reader.HasRows Then
      While reader.Read()
              Response.Expires = 0
              Response.Buffer = True
              Response.Clear()
              Response.Charset = "utf-8"
              Response.ContentType = "image/gif"
              Response.BinaryWrite(reader("image_file"))
              Response.Flush()
        End While
Else
        Response.Write("No image found!")
End If
reader.Close()
reader = Nothing
Conn.Close()
Conn = Nothing
Response.End()
0
robossaAuthor Commented:
Bilson! As I said  the image is shown already but using  binary write. What I want is to place the image in an <img> tag, such as <img src=?????? border="1" />

Thnaks
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

billsonCommented:
if you want to display by <img> tag, you require to save your binary data in image file on your server anyway and reference it to src="/image.gif"
0
robossaAuthor Commented:
How do I save the binary data in an image file?
0
billsonCommented:
why don't you use

 <img src="showimage.aspx?para1=1" border="1" /> to show your binary image directly

from aspx where showimage.aspx is the script above.
0
robossaAuthor Commented:
Thanks. If there is not picture for para1=1 what would be the decision making code to add a picture of "not available".  
0
billsonCommented:
You can save your binary image as below

Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Dim objStream
Dim FileName

FileName = reader("file_name"))
objStream = CreateObject("ADODB.Stream")
objStream.Type = adTypeBinary
objStream.Open()
objStream.Write(reader("image_file"))
objStream.SaveToFile(Server.MapPath ("/") & FileName , adSaveCreateOverWrite)
objStream = Nothing
response.write "<img src=""/""" &  FileName  & " border=""1"" />"

But I don't think it is good to store those duplicated files.
0
billsonCommented:
when there is no match, you can return ImageNotFound.gif

Dim objStream, FileName
Const adTypeBinary = 1

FileName = Server.MapPath("/") & "ImageNotFound.gif"

objStream = Server.CreateObject("ADODB.Stream")
objStream.Open()
objStream.Type = adTypeBinary
objStream.LoadFromFile(FileName)

Response.Expires = 0
Response.Buffer = True
Response.Clear()
Response.Charset = "utf-8"
Response.ContentType = "image/gif"
Response.BinaryWrite(objStream.Read)
Response.Flush()
objStream = Nothing
Response.End()
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.