如何显示数据库中一般方法无法读取的JPEG图片

具体情况是这样的:数据库是sql server2000,存储图片的字段类型是image.
数据库里的图片使用一个办公软件存储进去的,现在需要用网页的方式把它读取出来,但是这个方法读取不出来,

1   
2Response.Expires = 0   
3Response.Buffer = TRUE   
4Response.Clear   
5Response.ContentType = "image/gif"   
6Set rs= Server.CreateObject("ADODB.Recordset")   
7rs.open "select * from news where id="&Request.QueryString("id"),conn,1,2   
8Response.BinaryWrite rs("pic")   
9Response.End   

后来我看了一下,字段里包含了一些这样的信息:
C:\DOCUME~1\81187\桌面\电网快迅\1.jpg,是不是因为存在这样的东西我才读取不出来,请各位高手帮帮忙!

---------------------------------------------------------------

jpeg 图片文件开始的位置有一个 SOI marker , 其值是 FF D8 . 其他的一些文件格式也有类似的标志。你用 UltraEdit 打开一个 jpeg 文件就可以看的一清二楚。 下面是我找到的其他一个文件格式的资料:

What are the format identifiers of some popular file formats?

--------------------------------------------------------------------------------

Here are a few algorithms that you can use to determine the format of a
graphics file at run-time.

GIF: The first six bytes of a GIF file will be the byte pattern of
474946383761h ("GIF87a") or 474946383961h ("GIF89a").

JFIF: The first three bytes are ffd8ffh (i.e., an SOI marker followed
by any marker). Do not check the fourth byte, as it will vary.

JPEG: The first three bytes are ffd8ffh (i.e., an SOI marker followed
by any marker). Do not check the fourth byte, as it will vary.
This works with most variants of "raw JPEG" as well.

PNG: The first eight bytes of all PNG files are 89504e470d0a1a0ah.

SPIFF: The first three bytes are ffd8ffh (i.e., an SOI marker followed
by any marker). Do not check the fourth byte, as it will vary.

Sun: The first four bytes of a Sun Rasterfile are 59a66a95h. If you have
accidentally read this identifier using the little-endian byte order
this value will will be read as 956aa659h.

TGA: The last 18 bytes of a TGA Version 2 file is the string
"TRUEVISION-XFILE.\0". If this string is not present, then the file
is assumed to be a TGA Version 1 file.

TIFF: The first four bytes of a big-endian TIFF files are 4d4d002ah and
49492a00h for little-endian TIFF files.

 1   
 2'-------------------------------------------------------------------------------------   
 3' 函数:function ShowJpegField(field)   
 4' 作者: inelm(Archimond【阿克蒙德】) from csdn   
 5' Date: 2003-12-6 更新   
 6' 功能: 取得保存 jpeg 图片的字节数组中的 SOI marker 开始位置, 并从该位置输出真正的图片信息   
 7' 注: jpeg 格式的 SOI marker : FFD8   
 8' bmp 格式:424D   
 9' 参数: 图片字段   
10' 返回值: 无   
11' 调用范例:ShowJpegField(rs("picture1"))   
12' 注意: 调用此函数之前, 需要先申明 response.write 的 MIME 类型为 "image/jpeg"   
13'-------------------------------------------------------------------------------------   
14function ShowJpegField(field)   
15dim size, i, j   
16'要输出字段的总字节数   
17size = field.ActualSize   
18  
19'循环找到 SOI marker 的位置   
20for i = 1 to size   
21if AscB(MidB(field, i, 1)) = &HFF and AscB(MidB(field, i + 1, 1)) = &HD8 then   
22exit for   
23end if   
24next   
25  
26'忽略前面的无用信息, 从 SOI marker 开始输出真正的图片信息   
27for j = i to size   
28response.BinaryWrite MidB(field, j, 1)   
29next   
30end function   
 1   
 2'-------- 主程序开始 ------------------------------   
 3dim conn   
 4set conn = Server.CreateObject("ADODB.Connection")   
 5conn.open("Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=123;Data Source=MARK")   
 6  
 7sql = "select * from xinxi_mishuchu"   
 8set rs = conn.execute(sql)   
 9  
10'声明输出类型, 清空输出缓冲区   
11response.buffer = true   
12response.clear   
13response.ContentType = "image/jpeg"   
14  
15'调用函数输出图片   
16ShowJpegField(rs("picture1"))   
17  
18'打完收功!   
19rs.close : set rs = nothing   
20conn.close : set conn = nothing   
Published At
Categories with Web编程
Tagged with
comments powered by Disqus