Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-03-21
11
Medium Priority
?
281 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
0
Comment
Question by:robossa
  • 5
  • 4
9 Comments
 

Author Comment

by:robossa
ID: 16252427
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
 
LVL 1

Expert Comment

by:billson
ID: 16254782
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
 

Author Comment

by:robossa
ID: 16256309
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:billson
ID: 16258189
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
 

Author Comment

by:robossa
ID: 16259163
How do I save the binary data in an image file?
0
 
LVL 1

Expert Comment

by:billson
ID: 16259773
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
 

Author Comment

by:robossa
ID: 16259890
Thanks. If there is not picture for para1=1 what would be the decision making code to add a picture of "not available".  
0
 
LVL 1

Expert Comment

by:billson
ID: 16259937
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
 
LVL 1

Accepted Solution

by:
billson earned 1000 total points
ID: 16259993
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
How do you create a user-centered user experience on your website? And what are some things you should consider in the process?
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question