|
Edit an Existing ImageThe sample script loadimage.asp loads an existing jpg image, edits the image, and saves the edited image as a gif. (Note: You will find the original images for this and other samples in doc-samples\samples\tutorial\Images, and the generated images in doc-samples\samples\tutorial\GeneratedImages.) The Type Library ReferenceLoadimage.asp includes the public constants
saiNearestNeighbor,
saiBrowser, and
saiGIF.
To access ImgWriter's public constants from an ASP script, include a reference to
the component's type library in a
<!-- METADATA TYPE="TypeLib" UUID="{50D94450-589A-409B-819A-4F88B151C134}"-->
The UUID is a unique identifier for ImgWriter's type library. Creating an Instance of the ImageGen ObjectImgWriter's principal object is the image generator ImageGen. When you create an instance of ImageGen, it automatically creates an instance of the Font object. To create an instance of ImageGen use,
Set objImageGen = Server.CreateObject("softartisans.ImageGen")
Loading and Editing an ImageUse ImageGen's LoadImage method to load an existing image from a file, an ADO Recordset, or a byte array. Once the image is loaded, you can use ImageGen's methods and properties to edit it and save the new image. LoadImage takes one argument, the complete file path and name. The sample script loads olivetrees.jpg:
objImageGen.LoadImage Server.MapPath(Application("vroot") & "samples/tutorial/images/olivetrees.jpg")
ResizeImage can be used to change the dimension of the image: objImageGen.ResizeImage (objImageGen.Width)*2, (objImageGen.Height)*2, saiNearestNeighbor *Note: The method CreateThumbnail can also be used to easily resize and image.
SharpenImage sharpens the enlarged image: objImageGen.SharpenImage 10 To display text on an image, use the method DrawTextOnImage. Set properties of the Font object to customize the look of text. The sample script displays the image name on the image in bold, red, Verdana font: Set objFont = objImageGen.Font objFont.Color = rgb(221,0,0) objFont.Name = "verdana" objFont.Weight = 700 objImageGen.DrawTextOnImage 10, 10, 100, 1, "Olive Trees" The SaveImage method takes three arguments:
The sample file Loadimage.asp streams the generated image to the browser as a gif file, and provides "samples/tutorial/GeneratedImages/olivetrees.gif" as a preset path for saving:
objImageGen.SaveImage saiBrowser, saiGIF, Server.MapPath(Application("vroot") & _
"samples/tutorial/GeneratedImages/olivetrees.gif")
Wrapping UpIt is considered good programming practice to explicitly release a reference to an object when it will no longer be used. Ending the ASP response prevents the transfer of extraneous additional characters with the image. Set objImageGen = Nothing Response.end Note that there is no HTML in loadimage.asp. The ASP page consists only of server-side script. This is important because the generated image is contained in a single ASP response. If you add other elements to the response, the image may be corrupted. When streaming an image to the browser from an asp page, do not include HTML or calls to Response.Write lines.
Copyright © 2001-2004, SoftArtisans, Inc. |