Getting Started
  What is ImgWriter?
  Features and Benefits
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

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


[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

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:


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:


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.

Set rs = Nothing
Set conn = Nothing
Set objImageGen = Nothing

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.