We help IT Professionals succeed at work.

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

robossa
robossa asked
on
Medium Priority
299 Views
Last Modified: 2008-02-01
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
Comment
Watch Question

Author

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.

Commented:
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()

Author

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

Commented:
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"

Author

Commented:
How do I save the binary data in an image file?

Commented:
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.

Author

Commented:
Thanks. If there is not picture for para1=1 what would be the decision making code to add a picture of "not available".  

Commented:
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.
Commented:
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()

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.