Objects | data | database | In ASP, we often need to display a picture of the database stored in binary form on the Web page. The general picture shows no problem, because this article has a lot of, I will no longer elaborate. But sometimes the pictures in the database are entered through other Office software, or in other ways, such as directly in Access. At this point, the picture is stored in the database as an OLE object, and in front of the real content of the picture, some other information, such as the path and file name of the picture, and so on, are saved. If we still use the normal output method, it will make an error, resulting in the picture can not be displayed.
Fortunately, JPEG, BMP and other image format in the image content at the beginning of a SOI marker, this marker for JPEG is FFD8, and for BMP is 424D. So, we just have to find this marker position, we can ignore the previous content and directly from here to start output pictures. Take SQL server, for example, with the following code:
<%
'-------------------------------------------------------------------------------------
' Function: function Showjpegfield (field)
' Author: Inelm (archimond "Archimonde") from CSDN
' Date:2003-12-6 update
' function: Get the SOI marker start position in the byte array that holds the JPEG picture, and output the real picture information from that location
' Note: SOI marker:ffd8 in JPEG format
' BMP format: 424D
' Parameters: Picture fields
' Return value: None
' Invocation Example: Showjpegfield (RS ("Picture1"))
' Note: Before calling this function, you need to declare that the MIME type of Response.Write is ' image/jpeg '
'-------------------------------------------------------------------------------------
function Showjpegfield (field)
Dim size, I, J
' The total number of bytes to output the field
size = field. ActualSize
' Loops to find the location of SOI marker
For i = 1 to size
If AscB (MidB (field, I, 1) = &hff and AscB (MidB (field, i + 1, 1)) = &hd8 Then
Exit For
End If
Next
' Ignore the useless information in front, start outputting the real picture information from SOI marker
For j = i to size
Response. BinaryWrite MidB (Field, J, 1)
Next
End Function
%>
<%
'--------The main program starts------------------------------
Dim conn
Set conn = Server.CreateObject ("ADODB. Connection ")
Conn.Open ("Provider=SQLOLEDB.1; Password=sa; Persist Security info=true; User id=sa;initial catalog=123;data Source=mark ")
sql = "SELECT * FROM Xinxi_mishuchu"
Set rs = conn.execute (SQL)
' Declare the output type, empty the output buffer
Response.Buffer = True
Response.Clear
Response. ContentType = "Image/jpeg"
' Call function output picture
Showjpegfield (RS ("Picture1"))
' Finish the work!
Rs.close:set rs = Nothing
Conn.close:set conn = Nothing
%>
If the other format of the picture, such as BMP, GIF, and so on, the output method is similar, but is SOI marker different.
Because the author level is limited, the mistake place is unavoidable, welcome everybody criticism correction.