Solved

Displaying a Picture

Posted on 1998-09-08
25
323 Views
Last Modified: 2013-12-25
Hello I have an OLE Object field in an Access database.  This field has a JPEG picture saved in it.  what I need to do is display this image on my website using Active Server Pages.  Can someone give me code basically this is what I looking for a function that takes a parameter.  This parameter will be the field that the picture is located in.  The function will return the image exactly the way it should appear on the page, ie

<img src=<%=GetPicture(pic1)%> border=0>

That's what I want to be able to write and have the picture displayed.
0
Comment
Question by:Jagar
  • 12
  • 8
  • 3
  • +2
25 Comments
 
LVL 28

Expert Comment

by:sybe
ID: 1854827
1.

code for ShowImage.asp:

<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "DSN=YourDSN"

Response.ContentType = "image/GIF"

Set RSImage = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT imagefield FROM table WHERE id=" & Request.Querystring("id")
RSImage.Open strSQL, Conn, 1, 1
Response.Binarywrite RSImage("imagefield")
RSImage.Close
%>


2.

Call it with:
<img src="ShowImage.asp?id=2" border=0>

0
 
LVL 2

Author Comment

by:Jagar
ID: 1854828
That's not what I asked for I already have database connection open with the field and everything.  What I need is a function that will use the already open database connection.
0
 
LVL 28

Expert Comment

by:sybe
ID: 1854829
I am sorry, but I still think this is what you want. You have to define the image as an external file. The "src" in the image tag ALWAYS points to a URL.

I am afraid that it is not possible to write the binary code for the image in am HTML file. If it is, then, it's not really gonna be easy.

By the way, the database connection you have is not completely useless, because you have have to pass an ID to ShowImage.asp in the querystring. This ID should come from the recordset you have.

0
 
LVL 10

Expert Comment

by:MasseyM
ID: 1854830
Response.ContentType = "image/GIF"
Response.Binarywrite RSImage("imagefield")

This is from sybe... this is what you need to add to your "already open" database to get the file to display... Sybe, your answer is correct...

0
 
LVL 2

Author Comment

by:Jagar
ID: 1854831
OK SYBE I'll try that reenter you answer and I'll grade it after I check it out.  Sorry about the rejection, I wanted to do it that way, but I guess it's not possible.  Thank You.
0
 
LVL 2

Author Comment

by:Jagar
ID: 1854832
I'm having a problem I getting broken pictures.  Here is what I have
Here is the <A> tag, actually three of them side by side

<img src="ShowImage.asp?id=1&Which=1&Table=1" Border=0>
<img src="ShowImage.asp?id=1&Which=2&Table=1" Border=0>
<img src="ShowImage.asp?id=1&Which=3&Table=1" Border=0>

Here is ShowImage.asp
<%
Response.ContentType = "image/GIF"
Set RS = Server.CreateObject("ADODB.Recordset")
if Response.QueryString("Which") = 1 then
  Which="Pic1"
elseif Response.QueryString("Which") = 2 then
  Which="Pic1"
elseif Response.QueryString("Which") = 3 then
  Which="Pic1"
end if
if Response.QueryString("Table") = 1 then
  Table="Directors"
elseif Response.QueryString("Table") = 2 then
  Table="AirParks"
elseif Response.QueryString("Table") = 3 then
  Table="Properties"
end if
strSQL = "SELECT " & Which & " FROM " & Table & " WHERE id=" & Request.Querystring("id")

RSImage.Open strSQL, "DSN=AirMLS;uid=" & Session("uid") & ";pwd=" & Session("pwd"), 1, 1
Response.Binarywrite RSImage("imagefield")
RSImage.Close %>


Any idea why.
0
 
LVL 28

Expert Comment

by:sybe
ID: 1854833
It all depends on the database now.

If you show me the which tables you have and which fields those tables have, and which field the image is in, then I can help you.

The code looks well, accept I cannot check if the strSQL is valid for the database you use.




0
 
LVL 28

Expert Comment

by:sybe
ID: 1854834
I guess you got it working now, because you don't have anymore questions. So it's time to get my points :)
0
 
LVL 2

Author Comment

by:Jagar
ID: 1854835
Actually I had to leave work early yesterday, so I never was able to get you response until today.  I have not fixed it.  Do you want me to E-Mail you the .MDB file or just tell you.  If I tell its gonna be exactly what is written.  I have three pictures fields Pic1, Pic2, Pic3.  In three different tables, Directors, Properties, and Airparks.
I know in the above code it says Pic1, Pic1, Pic1, but I've changed that.  Also all the fields and stuff are valid, because otherwise I would get and error when it tried to use that as a SQL string, but I am not getting and error, just broken pictures.
I won't reject your answer because Massey said that it was correct, but I can't award the points without it working correctly.
0
 
LVL 28

Expert Comment

by:sybe
ID: 1854836
I understand that.
There is a way to see the ASP error in the picture. You can do that by rightclicking on the (broken) image and choose "View Image". It then opens in your browser, it will still be broken, but now you can do "view source". There you get the error message.

Do that and show the error message here.
0
 
LVL 2

Author Comment

by:Jagar
ID: 1854837
Thanks I never knew that, but you wanna know something you can't do that in IE.  Here is waht the error says

Type mismatch: '[undefined]'
/airmls/ShowImage.asp, line 21</font>

Actually there was another error first, but I fixed that one.  I don't know about this one though.  Any Ideas
0
 
LVL 28

Expert Comment

by:sybe
ID: 1854838
Hmm, supposing that line 21 points to this line:
"Response.Binarywrite RSImage("imagefield") "

Then you should replace "imagefield" with the fieldname in the database which contains the image.
In you sql statement you do "SELECT which FROM..."
So I suppose that the field containing the image is called "which"

Line 21 should be then:
"Response.Binarywrite RSImage("which")"

===========

Another debuggng tip: If you want to see the error messages:

You can call the image directly in you browser by typing in the url of the image:

http://yourserver/yourpath/ShowImage.asp?id=1&Which=1&Table=1

In ShowImage.asp you can comment out (temporarily) the line that says:
Response.ContentType = "image/GIF"
(put an apostroph before it)
Then the browser will think it is a html output and any errors can be read. If there are no errors, you will see the binary code for the image. Then it is time to uncomment the "Response.ContentType" line. After you have done that, and you reload, you should see the image.





0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Author Comment

by:Jagar
ID: 1854839
I've made all the changes, but am still getting broken pictures.  I ran the line
http://computername/airmls/ShowImage.asp?id=1&Which=1&Table=1
and I don't get any errors.  

Now here is a question for you; I use MS Access 7.0 as my database and have an OLE Object field for the Pics.  I double clicked the field and said to insert the object from file and then selected the file that I wanted to be displayed.
0
 
LVL 2

Author Comment

by:Jagar
ID: 1854840
I changed the way I added the image.  No I used VB code to copy the file to the database.
I'm really stumped here got any ideas.

Here is the VB code that I used

Public Function SaveFileToDB(Source As String, BynaryField As Field) As Integer
  ' *** Will save a file to a Binary field
  Dim nNumBlocks       As Integer
  Dim nFile            As Integer
  Dim nI               As Integer
  Dim nFileLen         As Long
  Dim nLeftOver        As Long
  Dim sFileData        As String
  Dim RetVal           As Variant
  Dim nBlockSize       As Long
   
  On Error GoTo Error_SaveFileToDB
 
  nBlockSize = 32000    ' Set size of chunk.
   
  ' *** Open the source file.
  nFile = FreeFile
  Open Source For Binary Access Read As nFile
   
  ' *** Get the length of the file.
  nFileLen = LOF(nFile)
  If nFileLen = 0 Then
    SaveFileToDB = 0
    Exit Function
  End If
   
  ' *** Calculate the number of blocks to read and nLeftOver bytes.
  nNumBlocks = nFileLen \ nBlockSize
  nLeftOver = nFileLen Mod nBlockSize
   
  ' *** Read the nLeftOver data, writing it to the table.
  sFileData = String$(nLeftOver, 32)
  Get nFile, , sFileData
  BynaryField.AppendChunk (sFileData)
   
  ' *** Read the remaining blocks of data, writing them to the table.
  sFileData = String$(nBlockSize, Chr$(32))
  For nI = 1 To nNumBlocks
    Get nFile, , sFileData
    BynaryField.AppendChunk (sFileData)
  Next
  Close #nFile
  SaveFileToDB = nFileLen
   
  Exit Function

Error_SaveFileToDB:
  SaveFileToDB = -Err
  Exit Function
end function

0
 
LVL 28

Expert Comment

by:sybe
ID: 1854841
Yes, I know, putting a picture as an OLE object in an OLE field, does not work.

I use an upload module to put images in a database. SA-FileUp from SoftArtisans (www.softartisans.com), there is a free trial version available.

At first I used also a VBA-script to get an image into a database field. I have to find it, but I think it used the GetChunk method.

0
 

Expert Comment

by:clsmaster
ID: 1854842
That's what the above function uses actually AppendChunk to add and GetChunk to read from the database.  I need to be able to this programactically.
0
 
LVL 2

Author Comment

by:Jagar
ID: 1854843
I done everything you said and still don't get pictures displayed.
0
 
LVL 28

Expert Comment

by:sybe
ID: 1854844
The problem is I think with the contents of the database. Have you tried using the upload module from www.softartisans.com. ? That will put the image in the database in the right format.

I know I had some VBA code to put the image in the database, and that code worked, but I am afraid that I can't find it anymore.

0
 
LVL 2

Author Comment

by:Jagar
ID: 1854845
I have a routine that adds the picture to the database and displays it in Visual Basic.  It  does this in JPEG format and these work correctly.
0
 

Accepted Solution

by:
millarch earned 50 total points
ID: 1854846
Most Internet browsers support the displaying of GIF and JPEG images. To display an image, the browser requests the image from a Web server. The server passes the image to the browser as an HTTP transaction with an HTTP header containing an MIME type of IMAGE/GIF or IMAGE/JPEG. You can simulate this behavior with Active Server Pages.

The following example constructs an HTTP header for an image and then uses the binary information from an image field in SQL Server to provide a GIF to the browser.


   FILE: SHOWIMG.ASP
   <%@ LANGUAGE="VBSCRIPT" %>
   <%
   ' Clear out the existing HTTP header information
   Response.Expires = 0
   Response.Buffer = TRUE
   Response.Clear

   ' Change the HTTP header to reflect that an image is being passed.
   Response.ContentType = "image/gif"

   Set cn = Server.CreateObject("ADODB.Connection")
   ' The following open line assumes you have set up a System DataSource
   ' by the name of myDSN.
   cn.Open "DSN=myDSN;UID=sa;PWD=;DATABASE=pubs"
   Set rs = cn.Execute("SELECT logo FROM pub_info WHERE pub_id='0736'")
   Response.BinaryWrite rs("logo")
   Response.End
   %>

This script only displays an image on the screen. If you wish to display an image from an HTML or ASP document you must refer to this script in an image tag. For example, if you wished to display this image with a caption describing it, you might use the following HTML page:

   <HTML>
   <HEAD><TITLE>Display Image</TITLE></HEAD>
   <BODY>
   This page will display the image New Moon Books from a SQL Server 6.5
   image field.<BR>
   <IMG SRC="SHOWIMG.ASP">
   </BODY>
   </HTML>

NOTE: The ASP script assumes that the image field (BLOB Data) in the SQL Server table contains a raw GIF image. Internet browsers assume that raw GIF or JPEG data follow the HTTP header. If any extraneous information is contained in the BLOB data, this will be passed by this script, and the image will not display properly. This becomes important when you realize that most methods of placing images into BLOB fields place extra information in the form of headers with the image. Examples of this are Microsoft Access and Microsoft Visual FoxPro. Both of these applications save OLE headers in the BLOB field along with the actual binary data.
This technique can also be applied to other types of binary data, not just graphics. The browser needs to know what type of content is being presented. Do this by specifying the proper mime type in the Response.ContentType variable. For example, if you wanted to view a word document, you would set the ContentType = "application/msword".
0
 
LVL 2

Author Comment

by:Jagar
ID: 1854847
Why does not the following code work.  It seems to do everything you said above

<%
Response.ContentType = "image/JPEG"
Set RS = Server.CreateObject("ADODB.Recordset")
if Response.QueryString("Which") = 1 then
  Which="Pic1"
elseif Response.QueryString("Which") = 2 then
  Which="Pic1"
elseif Response.QueryString("Which") = 3 then
  Which="Pic1"
end if
if Response.QueryString("Table") = 1 then
  Table="Directors"
elseif Response.QueryString("Table") = 2 then
  Table="AirParks"
elseif Response.QueryString("Table") = 3 then
  Table="Properties"
end if
strSQL = "SELECT " & Which & " FROM " & Table & " WHERE id=" & Request.Querystring("id")

RSImage.Open strSQL, "DSN=AirMLS;uid=" & Session("uid") & ";pwd=" & Session("pwd"), 1, 1
Response.Binarywrite RSImage("imagefield")
RSImage.Close %>
0
 

Expert Comment

by:millarch
ID: 1854848
1) You didn't clear the HTTP first:
Response.Expires = 0
   Response.Buffer = TRUE
   Response.Clear

2) If you are using MS Access, it adds up more bits to the beginning of the OLE field, so the image is not raw data. You'd need to use SQL Server or another database that only saves the image data itself in the image field.
0
 
LVL 2

Author Comment

by:Jagar
ID: 1854849
If number two is right then how come I can use a similar function from within Visual Basic and load and save the pictures.
0
 

Expert Comment

by:millarch
ID: 1854850
Visual Basic is database oriented, and it knows you are dealing with a OLE field. With Internet Browsers the story is a bit different. What you are doing with your code is translating the OLE field into a data stream of bits, that is the reason for the page named SHOWIMG.ASP. When you call <IMG SRC="SHOWIMG.ASP"> it has no idea whatsoever you are dealing with a database or a OLE object. It is expecting a plain picture.
0
 
LVL 2

Author Comment

by:Jagar
ID: 1854851
I give you the points, but I can't get this to work because of the configuration that I'm forced to used.  Thank You for all your help
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now