How to display JPEG pictures stored as OLE objects in a database

Source: Internet
Author: User
Tags ole
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.



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.