|
The Image Collection ObjectLoading ImagesThe sample script imagecollection.asp uses the method LoadImage to add images to the image collection. (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.) Creating an Instance of the Images ObjectLoadImage is a method of the image collection object Images. The image cache can be used at Application, Session, or Page scope. This sample will create an instance of the object in the Application_OnStart method(found in global.asa) and store the reference in a Session variable:
Sub Application_OnStart()
Set Application("SAImgs") = Server.CreateObject("SoftArtisans.Images")
End Sub
Load ImagesThis reference can then be used in other ASP pages to add images to the cache. LoadMethod allows you to add an image to the collection from a file, an ADO Recordset field, or a binary array. The sample checks that the key does not already exist in the collection and loades three images into the collection from files:
If Application("SAImgs").Exists("CityHall") = False then
Application("SAImgs").LoadImage "CityHall", Server.MapPath(Application("vroot") & _
"samples/tutorial/images/stadshus.jpg")
End If
If Application("SAImgs").Exists("ConsertHall") = False then
Application("SAImgs").LoadImage "ConsertHall", Server.MapPath(Application("vroot") & _
"samples/tutorial/images/konsert.jpg")
End If
If Application("SAImgs").Exists("Square") = False then
Application("SAImgs").LoadImage "Square", Server.MapPath(Application("vroot") & _
"samples/tutorial/images/storget.jpg")
End If
Access Binary DataOnce an image has been added to the collection, it can be access in another page using array syntax with the image's name as the key. The sample uses and HTML img tag to add an image to the page. The img tag's src attribute points to the ASP page where the image will be retrieved from the image collection:
<IMG SRC="GetSquare.asp"> In GetSquare.asp, the image data is written to the ASP Response object:
Response.Buffer = True
Response.ContentType = "image/jpeg"
Response.BinaryWrite Application("SAImgs")("Square")
Response.End
Wrapping UpIt is considered good programming practice to explicitly destroy the object when it is no longer necessary. Since we have stored an object reference in a Session variable in Session_OnEnd, we should destroy it in Session_OnEnd:
Sub Application_OnEnd()
Set Application("SAImgs") = Nothing
End Sub
Copyright © 2001-2004, SoftArtisans, Inc. |