How to read graphics from an Access database (cont.)

Source: Internet
Author: User
Tags ole variable access database
access| Data | database | Graphics Note that you can also create a new bitmap object from a file, but use this
The way the graphic format is stored in the database is not recognizable by the browser.
When you save the image to the database, you should know what format you need to use to save
You can save every byte of the file, or save it by Access/foxpro the graphic
to an OLE format.
What format you use to save the image determines what format you use in the ASP to read the graphics.
Specifically, if you save the graphic as a bmp,gif,jpeg in Access/foxpro (
This must be used to access/foxpro OLE objects, that is, to use the Insert object of Access
dialog box to complete), which is when you use
Browsers are not able to explain when image/bmp.

Now let's assume that you have the graphic format you want to save in the database
(GIF, JPEG, BMP, TIFF, and so on) Now let's see how we can get them out of
Database to read it.

Two key techniques are used in access to save graphics
1. BMP format used
2. 78-byte file header

<%
Response. Expires = 0
Response. Buffer = True
Response. Clear
Response.ContentType = "Image/bmp"
%>
Then all you have to do is get rid of the file headers of the 78-byte OLE object.
<%
Const oleheadersize = 78
Nfieldsize = rs ("photo"). ActualSize
Oleheader = rs ("photo"). GetChunk (Oleheadersize)
Imagebytes = rs ("photo"). GetChunk (Nfieldsize-oleheadersize)
Response.BinaryWrite Imagebytes
%>

Now for an example:
If you want to get a worker's information, this information includes an introduction and his image.
And you want to display both text and graphics.
The code is as follows: (The theimg is a proxy page)
Theimg.asp
<%
Response. Expires = 0
Response. Buffer = True
Response. Clear
Response.ContentType = Session ("imagetype")
Response. BinaryWrite session ("Imagebytes")
Session ("imagetype") = ""
Session ("imagebytes") = ""
Response. End
%>


Function setimagefordisplay (field, ContentType)
Oleheadersize = 78
ContentType = LCase (ContentType)
Select Case ContentType
Case "GIF", "JPEG", "BMP"
ContentType = "image/" & ContentType
bytes = Field.value
Case "Ole"
ContentType = "Image/bmp"
nfieldsize = field. ActualSize
Oleheader = field. GetChunk (Oleheadersize)
bytes = field. GetChunk (Nfieldsize-oleheadersize)
End Select
Session ("imagebytes") = bytes
Session ("imagetype") = ContentType
End Function
' Note that only 4 of the formats are used in the program: GIF, JPEG, BMP, OLE.

<%
sql = "SELECT * FROM Employees"
Set ORS = Server.CreateObject ("ADODB. Recordset ")
Ors.cursorlocation = 3
oRS.Open sql, "DSN=NW"
Setimagefordisplay ORS ("photo"), "Ole"
Set ors.activeconnection = Nothing
%>

To display an image, you only need to be in another ASP, assuming that the getempinfo.asp

But there's another problem, because the same "theimg.asp" is used for each employee's graph.
File, you should change it a little bit:


Finally, how do you show multiple images?
That is, what if there are more than one field in the database that holds the graphic?
In fact, the solution is very simple, just give Setimagefordisplay an extra parameter
is a session variable used to hold the graph.
For example:
Setimagefordisplay oRS1 ("photo"), "Ole", "Empphoto"
Setimagefordisplay oRS2 ("logo"), "gif", "Complogo"




Use this method to complete the following functions:
1. Ability to remove graphics fields from the database. (The only thing you need to know is what format the graphics in the database are.)
Bmp?gif?jpeg?ole?)
2. Use the session variable to save the number of graphics and content type
The ASP needs this information to connect to the properties in
3. Just put the theimg where you want the graphics to appear, and you'll be able to display the image.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.