aspjpeg缩小图片的组件使用介绍(1)

发表于:2007-06-30来源:作者:点击数: 标签:
AspJpeg 1.1 Programmer‘’s Manual Copyright (c) 2000 All Rights Reserved. This software is based in part on the work of the Independent JPEG Group (IJG) What is AspJpeg? AspJpeg is an active server component that creates resized versions

AspJpeg 1.1 Programmer‘’s Manual

Copyright (c) 2000
All Rights Reserved.
This software is based in part on the work of the Independent JPEG Group (IJG)
What is AspJpeg?
AspJpeg is an active server component that creates resized versions of JPEG images. AspJpeg enables your ASP application to dynamically create high-quality thumbnails of images in just a few lines of code.
This component can be used in tandem with , the leading file upload component from , to create thumbnails of images as they are being uploaded.
System Requirements
Windows 95/98/NT/2000 and
IIS 4.0+ and ASP or
Visual Basic 5.0+ or
Visual C++ 5.0+ or
any development environment supporting COM.
Installation
The setup application aspjpeg.exe sets up and registers the AspJpeg component along with documentation and samples in a directory of your choice. To install AspJpeg on another computer, it is sufficient to copy the file aspjpeg.dll to the other system and register it there using the regsvr32 command-line utility as follows:
c:\>regsvr32 c:\path\aspjpeg.dll
If that machine was already running an older version of the component, you should shut down and restart the IIS services to remove the old version of the DLL from memory, as follows:
c:\>net stop iisadmin /y
c:\>net start w3svc
The setup installs a 30-day evaluation copy of the AspJpeg component. The evaluation version is fully functional, i.e. no features are disabled or limited. After 30 days, the component will start throwing an expiration error unless/until a registration key is installed. A registration key can be purchased at the web site. Once a key is obtained, you can import it into the system registry without re-installing the component. Your web server does not need to be shut down.
Getting Started
The AspJpeg.exe[/B] setup application attempts to create the virtual directory /AspJpeg on top of the targetdir\Samples directory of the installation. Make sure it has been created suclearcase/" target="_blank" >ccessfully. If necessary, recreate it manually using File Explorer:
Right-click on the \Samples sub-directory underneath the target directory (typically it is c:\Program Files\Persits Software\AspJpeg unless otherwise specified), select Properties, open the Web Sharing tab, choose "Share this folder", type in AspJpeg under Alias. Click OK.
Also, if you installed AspJpeg on an NTFS drive, make sure the \Samples\images directory grants Everyone Full Control, or the sample ASP scripts will not be able to write to it. Using Windows Explorer, right-click on the directory targetdir\Samples\images, select Properties/Security, and if necessary, add the entry Everyone/Full Control.
Your First Thumbnail
To create a thumbnail of a JPEG image using AspJpeg, you should take the following simple steps:
  • Create an instance of the AspJpeg object via Server.CreateObject (or New if used in VB).
  • Open your image file via the Jpeg.Open method.
  • Set new image dimensions via the properties Jpeg.Width and Jpeg.Height. You may use read-only properties Jpeg.OriginalWidth and Jpeg.OriginalHeight to preserve the original width/height ratio.
  • Optional: You may include calls to any or all of the following methods: Jpeg.Sharpen to apply sharpening to the resultant thumbnail, Jpeg.Crop to crop it, Jpeg.FlipV and Jpeg.FlipH to flip an image vertically and/or horizontally.
  • Save the resultant thumbnail to disk via the Jpeg.Save method, or send it directly to the browser via the Jpeg.SendBinary method.
The code sample demonstrates this procedure on the sample image clock.jpg located in the \images sub-folder.
Here is the underlying ASP script:
<% ‘’ Create instance of AspJpeg
Set Jpeg = Server.CreateObject("Persits.Jpeg")
‘’ Compute path to source image
Path = Server.MapPath("images") & "\clock.jpg"
‘’ Open source image
Jpeg.Open Path
‘’ Decrease image size by 50%
Jpeg.Width = Jpeg.OriginalWidth / 2
Jpeg.Height = Jpeg.OriginalHeight / 2
‘’ Apply sharpening if necessary
‘’ Jpeg.Sharpen 1, 130
‘’ create thumbnail and save it to disk
Jpeg.Save Server.MapPath("images") & "\clock_small.jpg"
%>
<IMG SRC="images/clock.jpg"><P>
<IMG SRC="images/clock_small.jpg">

This code snippet opens the image clock.jpg located in the images subdirectory, creates a reduced version of the image, and saves it in the same directory under the name clock_small.jpg. It then displays both images via <IMG> tags.
Sending Thumbnails Directly to the Browser
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 Jpeg.SendBinary. This method is similar to Jpeg.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 Jpeg.SendBinary method must be called from a separate ASP script which contains no HTML tags whatsoever. This script should be invoked via the SRC attribute of an <IMG> tag.
The code sample contains several <IMG> tags invoking the script sendbinary.asp and passing file path and size parameters to it, as follows:
<IMG SRC="sendbinary.asp?path=<% = Path %>&width=300">
Here is the file sendbinary.asp:
<%
‘’ Create an instance of AspJpeg object
Set jpeg = Server.CreateObject("Persits.Jpeg")
jpeg.Open( Request("path") )
‘’ Set new width
jpeg.Width = Request("Width")
‘’ Set new height, preserve original width/height ratio
jpeg.Height = jpeg.OriginalHeight * jpeg.Width / jpeg.OriginalWidth
‘’ Send thumbnail data to client browser
jpeg.SendBinary
%>

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.
Using AspJpeg in Tandem with AspUpload
AspUpload enables your ASP application to capture, store and process files uploaded with a browser. An on-line photo album, or any other web-based image repository which allows users to upload photographs in the JPEG format, can benefit greatly from the AspUpload/AspJpeg tandem.
The code sample demonstrates how you can create a thumbnail of an image uploaded with a browser and store both the original image and its thumbnail in the database as blobs. The file form.asp contains a form which allows you to select an image for uploading, specify the size of a thumbnail and image description. This form invokes the script upload.asp which captures the uploaded file, creates a thumbnail and stores both images along with the description in the sample Microsoft Access database located in the folder \Samples\db of the installation.
To learn more about AspUpload, download and install your free evaluation copy from .
Setting Resizing Algorithms
AspJpeg supports three popular image resizing algorithms: Nearest-Neighbor, Bilinear and Bicubic.
The Nearest-Neighbor algorithm is the fastest of the three, but produces relatively low-quality thumbnails. The Bilinear algorithm offers a much better quality but is about twice as slow as Nearest-Neighbor. And finally, the Bicubic algorithm provides the highest quality but is approximately twice as slow as Bilinear and, therefore, 4 times as slow as Nearest-Neighbor.
A resizing algorithm for your thumbnails can be specified via the property Jpeg.Interpolation. Valid values for this property are: 0 (Nearest-Neighbor), 1 (Bilinear), and 2 (Bicubic). The default value is 1 (Bilinear). For this property to take effect, it must be set before calling Save or SendBinary.
The following table illustrates the effect of setting the Interpolation property. Notice how much smoother the thumbnails produced by the Bilinear and Bicubic algorithms are in comparison to Nearest-Neighbor. Notice also that in many cases (such as this one) Bicubic provides little, if any, improvement over Bilinear.
Algorithmjpeg.InterpolationEffect
Nearest Neighbor0
Bilinear1
Bicubic2
Image Sharpening New in Version 1.1
Starting with Version 1.1, AspJpeg is capable of applying a sharpening filter to an image being resized via the method Sharpen. A regular thumbnail and two thumbnails with various degrees of sharpening applied to them are shown below.
[TD]
No sharpening Sharpen(1, 120)Sharpen(1, 250)

The Sharpen method uses two Double arguments: Radius and Amount. Radius controls the size (in pixels) of an area around every pixel that the sharpening algorithm examines. This argument should normally be set to 1 or 2. Amount (expressed in %) specifies the degree of sharpness. This argument must be greater than 100.
Image Cropping New in Version 1.1
AspJpeg 1.1+ is also capable of cutting off edges from, or cropping, the resultant thumbnails via the method Crop(x0, y0, x1, y1). The size of the cropped image is specified by the coordinates of the upper-left and lower-right corners within the resultant thumbnail, not the original large image.

Image Flipping New in Version 1.1
With AspJpeg 1.1, you can invert an image horizontally and/or vertically by calling the methods FlipH and FlipV, respectively.

原文转自:http://www.ltesting.net