Getting Started
  Introduction
  What is ImgWriter?
  Features and Benefits
  Requirements
 
Quick Start
  Edit an Existing Image
  Create a New Image
  Save an Image in a Database
  The Image Collection Object

Programmer's Reference
  Object Model
      SAImageGen
          ImageGen
          Font
      SAImageCollection
          Images
          Image

Installation
  Installing ImgWriter

External Links
  ImgWriter home page
  Technical Support
  ImgWriter live demos
  SoftArtisans Home Page
  E-mail General Questions
  E-mail Technical Support

Edit an Existing Image

The 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.)

tutorial/loadimage.asp

[Run Sample] | [View Source]

The Type Library Reference

Loadimage.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"--> directive, as follows.

<!-- 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 Object

ImgWriter'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 Image

Use 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.

objImageGen.CreateThumbnail 150

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: SaveMethod, ImageFormat, and, optionally, SaveData. SaveMethod specifies whether to stream the image to the browser, save it as a file, save it in memory, or save it as a database blob. ImageFormat sets the image format. SaveData assigns a file path and name for saving. When saving the image as a file (SaveMethod = saiFile), you must include SaveData. When streaming the image to the browser (SaveMethod = saiBrowser), SaveData is optional, and provides a preset path and name, in case the user chooses to save the image. Do not set SaveMethod when saving the image in memory (SaveMethod = saiMemory).

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 Up

It 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.