When used in an IIS/ASP environment, AspJpeg is capable of sending a resultant thumbnail
directly to the browser without creating a temporary file on the server.
This is achieved via the method SendBinary. This method is similar to Save but instead of
saving a thumbnail to disk, it internally makes calls to ASP's Response.BinaryWrite method,
thereby sending the image to the client browser.
The SendBinary method must be called from a separate script file which should contain no HTML tags
whatsoever. This script should be invoked via the SRC attribute of an <IMG> tag.
The code sample 02_manythumbs.asp contains several <IMG> tags invoking the
script 02_sendbinary.asp and passing file path and size parameters to it, as follows:
<IMG SRC="02_sendbinary.asp?path=<% = Path %>&width=300">
Below are the ASP/VB Script and ASP.NET/C# versions of the 02_sendbinary.asp script.
Notice that the width of the thumbnail is passed via the query string variable Width,
and the height is set to be proportional to the specified width.
Once again, a script calling Jpeg.SendBinary must not
contain any HTML tags, or the data stream it generates will be corrupted.
Under .NET, you must use the directive <%@ Page aspCompat="True" %>
for the SendBinary method to work properly.
VB Script:
<%
Response.Expires = 0
' create instance of AspJpeg
Set Jpeg = Server.CreateObject("Persits.Jpeg")
' Open source file
Jpeg.Open( Request("path") )
' Set new height and width
Jpeg.Width = Request("Width")
Jpeg.Height = Jpeg.OriginalHeight * Jpeg.Width / Jpeg.OriginalWidth
' Perform resizing and
' send resultant image to client browser
Jpeg.SendBinary
%>
|
C#:
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="ASPJPEGLib" %>
<%@ Page aspCompat="True" %>
<script runat="server" LANGUAGE="C#">
void Page_Load(Object Source, EventArgs E)
{
// this script may not contain any HTML tags, not even comments
ASPJPEGLib.IASPJpeg objJpeg;
objJpeg = new ASPJPEGLib.ASPJpeg();
// Open source image
objJpeg.Open( Request["path"] );
// Set new width
objJpeg.Width = int.Parse(Request["width"]);
// Preserve aspect ratio
objJpeg.Height = objJpeg.OriginalHeight * objJpeg.Width / objJpeg.OriginalWidth;
// Send thumbnail data to client browser
objJpeg.SendBinary(Missing.Value);
}
</script>
|
Click the links below to run this code sample: