Using csImageFile with ASP.NET

csImageFile can be used with ASP.NET. The component must be registered on the server in the usual way and it can be called using Server.CreateObject. We have more details on component registration and running a COM component through component services on a 64 bit OS.

Example code to call the component in VB.NET:

Dim Image = Server.CreateObject("csImageFile.Manage")

Note the use of Dim compared with Set in classic ASP. For the trial version the class name is "csImageFileTrial.Manage"

The above code uses late binding. A more efficient way of calling the component is to use early binding, which means creating a .NET equivalent assembly using the TLBIMP tool. Click here for more.

There are some incompatibilities between the data structures used by csImageFile and those of .NET, the most important being the method used to stream an image to the browser. This can no longer be done in a single line but the example below shows how it can be done in VB.NET.

<%@ Page language="vb" debug=true %>
  Response.Expires = 0
  Response.Buffer = true

  Dim Image = Server.CreateObject("csImageFile.Manage")

  Image.NewImage(150, 50, "00ff00")
  Image.TextOpaque = false
  Image.TextSize = 22
  Image.Text(5, 10, "Sample image")

  Dim OutArray As Array = Image.GIFData
  Dim ByteArray(OutArray.Length - 1) As Byte
  Array.Copy(OutArray, ByteArray, OutArray.Length)
  Response.ContentType = "image/gif"

The last five lines of code take the image from csImageFile and stream it to the browser as a GIF. The output from the GIFData property is read into OutArray which is then copied into an array of bytes called ByteArray. This array of bytes can be used in the Response.BinaryWrite command. In classic ASP the BinaryWrite command could take the value of GIFData directly.

Sample Image

This is the sample image produced:

There are other differences in syntax between VBScript and VB.NET, mostly involving the use of brackets. In VBScript the NewImage and Text commands would not use brackets, but they do in VB.NET.

The code for streaming the image in C# is:

  Array OutArray = (Array)(Image.GIFData);
  Byte[] ByteArray = new Byte[OutArray.Length];
  Array.Copy(OutArray, ByteArray, OutArray.Length);
  Response.ContentType = "image/gif";

With C#, early binding must be used.