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

Save to Database

The sample script database.asp uses the method LoadImage to open an image from a Microsoft Access database, CropImage to modify the image, the image is and SaveImage to save a modified image to the database. ImgWriter also allows you to save an image to a database through the Images interface: SaveImage. (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/database.asp

[Run Sample] | [View Source]

The Type Library Reference

As in the previous example, the script contains a reference to the component's type library in a <!-- METADATA Type="TypeLib"--> directive, to enable access to the components public constants.

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

SaveImage is a method of the image generator object ImageGen. The first parameter, "SaveMethod", allows you to save the image to a database blob.

Set objImageGen = Server.CreateObject("softartisans.ImageGen")

Load an Image

After opening a database connection and a ADO Recordset:

Set conn = Server.CreateObject("ADODB.Connection") 
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
	"Data Source=" & Server.MapPath(Application("vroot") & "samples/tutorial/data/Images.mdb") & _
    "Persist Security Info=False"
conn.Open strConn
Set rs = Server.CreateObject("ADODB.Recordset")
rs.CursorType = adOpenKeyset
rs.LockType = adLockOptimistic
rs.Open "Images", conn, , , adCmdTable
rs.MoveFirst

The sample uses the method LoadImage to load an image from a database blob:

objImageGen.LoadImage rs.Fields("Image")

The sample then modifies the image using CropImage:

objImageGen.CropImage 0, 0, 60, 80

The edited image is displayed to the browser using SaveImage:

objImageGen.SaveImage saiBrowser, saiJPG, "C:\temp\house.jpg"

A new row is created in the ADO Recordset:

rs.AddNew

The row's "ImageTitle" field is set:

rs.Fields("ImageTitle") = "Small House"

The SaveImage method writes the image to the row's "Image" field:

objImageGen.SaveImage saiDatabaseBlob,saiJPG, rs.Fields("Image")

Calling the ADO Recordset objects "Update" method saves the new row to the database:

rs.Update

Wrapping Up

It is considered good programming practice to explicitly destroy the object when it is no longer necessary. Ending the ASP response prevents the transfer of extraneous additional characters with the image.

rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
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 generating an image, do not include HTML or Response.Write lines.


Copyright © 2001-2004, SoftArtisans, Inc.