2.3 从内存中打开图片Opening Images from Memory
从1.2版本开始,AspJpeg提供了OpenBinary方法,它能够从内存资源(例如一条ADO记录)中打开一张图片类同于打开一个磁盘文件。你能够使用这种方法来创建一个存在数据库里的图片的缩略图,你也可以用它打开上传到用AspUpload上传到内存中的图片。(这个主题将在下一章中展开讨论)Starting with version 1.2, AspJpeg offers the method OpenBinary which opens an image from a binary memory source (such as an ADO recordset) rather than a disk file. You can use this method to create thumbnails from images residing in a database as blobs. You can also use it to open images uploaded to memory with AspUpload (this topic is covered in the next chapter).
代码样例02_display.asp和02_fromdatabase.asp演示了OpenBinary方法。文件02_display.asp多次用不同的参数简单引用了另一段asp脚本02_fromdatabase.asp,文件02_fromdatabase.asp用类同于Open方法的Openbinary方法接收了一条ADO数据记录,这条数据记录的值以一定的算法包含了一张源图片。The OpenBinary method is demonstrated by the code samples 02_display.asp and 02_fromdatabase.asp. The file 02_display.asp simply invokes another ASP script, 02_fromdatabase.asp, multiple times with various parameters. The file 02_fromdatabase.asp uses the method OpenBinary rather than Open and passes an ADO recordset value containing a source image as an argument.
02_fromdatabase.asp/aspx的脚本代码如下所示The script 02_fromdatabase.asp/aspx is shown below:
VB Script:
<% ' Using ADO, open database with an image blob
strConnect = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("../db/aspjpeg.mdb")
Set rs = Server.CreateObject("adodb.recordset")
SQL = "select image_blob from images2 where id = " & Request("id")
rs.Open SQL, strConnect, 1, 3
Set Jpeg = Server.CreateObject("Persits.Jpeg")
' Open image directly from recordset
Jpeg.OpenBinary rs("image_blob").Value
' Resize
jpeg.Width = Request("Width")
' Set new height, preserve original aspect ratio
jpeg.Height = jpeg.OriginalHeight * jpeg.Width / jpeg.OriginalWidth
Jpeg.SendBinary
rs.Close
%>
C#:
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="ASPJPEGLib" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Page aspCompat="True" %>
<script runat="server" LANGUAGE="C#">
void Page_Load(Object Source, EventArgs E)
{
ASPJPEGLib.IASPJpeg objJpeg;
objJpeg = new ASPJPEGLib.ASPJpeg();
// Connect to database
String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("../db/aspjpeg.mdb");
OleDbConnection myConnection = new OleDbConnection(strConn);
myConnection.Open();
OleDbCommand myCommand = new OleDbCommand("select image_blob from images2 where id = " + Request["id"], myConnection);
OleDbDataReader myReader = myCommand.ExecuteReader();
myReader.Read();
// Open source image
objJpeg.OpenBinary( myReader["image_blob"] );
// Resize
objJpeg.Width = int.Parse(Request["Width"]);
// Set new height, preserve aspect ratio
objJpeg.Height = objJpeg.OriginalHeight * objJpeg.Width / objJpeg.OriginalWidth;
// Create thumbnail and send it directly to client browser
// aspCompat=True is required.
objJpeg.SendBinary( Missing.Value );
myReader.Close();
myConnection.Close();
}
</script>
点击以下链接来运行这些样例代码:Click the links below to run this code sample:
http://localhost/aspjpeg/manual_02/02_display.asp
http://localhost/aspjpeg/manual_02/02_display.aspx
2.4 输出到内存Output to Memory
除了Save和SendBinary方法之外,AspJpeg还提供了Binary属性,以二进制字节串列的形式返回生成的缩略图,这个属性允许你直接把缩略图保存到数据库里而不需要在磁盘上生成一个临时文件。In addition to the Save and SendBinary methods, AspJpeg also offers the Binary property which returns the resultant thumbnail as a binary array of bytes. This property allows you to save the thumbnail directly in the database without creating a temporary file on disk:
<%
...
Set rs = Server.CreateObject("adodb.recordset")
rs.Open "images", strConnect, 1, 3
rs.AddNew
rs("image_blob").Value = Jpeg.Binary
rs.Update
...
%>
下一章中将详述这一属性This property will be covered in detail in the next chapter.




上一篇
下一篇


文章来自:
Tags: 





