Creating thumbnail images using LS2J in LotusScript

Learn how to create thumbnail images in Lotus Domino using LS2J in LotusScript.

View member feedback to this tip.

This tip resolves a problem faced by many developers who code for the Web: how to create thumbnail images in Lotus Domino. Until I came up with this code, I either ended up with a solution that required manual steps or else I had to use an external program.

The code below uses LS2J to create thumbnail images in LotusScript. LotusScript does not natively have any methods for manipulating images, but Java does. LS2J (which is a part of Notes/Domino on the Win32 platform) makes it possible to expose Java functionality to LotusScript.

This tip employs an agent that is called in the WebQuerySave event of a Domino form. The agent, written in LotusScript, checks the document for image files, and if any are found, creates thumbnail files for them and attaches them to the document. You could create an agent for the Notes client that does the same thing. The central part of the agent is the line that calls the method that creates the thumbnail file. This method (thumbnailObject.ThumbnailThis in the code) receives four parameters:

  • The filename of the image that is to be used as the source for the thumbnail file.
  • The filename that the thumbnail image should have.
  • The maximum width of the thumbnail image.
  • The maximum height of the thumbnail image.

I have a form that holds a Body-field and an upload control to allow Web users to attach image files. The form has the following in the WebQuerySave event: @Command([ToolsRunMacro]; "SaveWeb")

In the database I have an agent with the following code:

Option Public
Option Declare
Uselsx "*javacon" 'Which lets you 
use Java from Lotusscript
Use "ThumbNail" ' A Java library that
 holds a function to do Thumbnailing 

Sub Initialize
 Dim session As New NotesSession
 Dim CurDB As NotesDatabase
 Dim curDoc As NotesDocument
 Dim fileList As Variant
 Dim expression As String, 
serverName As String, thumbnailPrefix As String
 Dim fileType As String, fileNameExcludingType As String
 Dim sourceFilePath As String, thumbFilePath As String
 Dim notesEmbeddedObject As NotesEmbeddedObject
 Dim bodyItem As NotesRichTextItem
 Dim thumbnailClass As JAVACLASS
 Dim thumbnailObject As JavaObject
 Set thumbnailClass = js.GetClass("ThumbNail")
 Set thumbnailObject = thumbnailClass.CreateObject
 Dim workingDir As String 
 Dim maxX As Integer, maxY As Integer
 Dim returnCode As String
 workingDir = "c:\temp\" 
'Hard coding - you should change this
 maxX = 100 'Maximum width of thumbnail in pixels
 maxY = 100 'Maximum height of thumbnail in pixels
 thumbnailPrefix = "t_" 'The prefix we will use for thumbnails
 Set curDb = session.CurrentDatabase
 servername = curDb.Server
 On Error Goto ErrorHandling
 expression = "@AttachmentNames"
 Set curDoc = session.DocumentContext
 Set bodyItem = curDoc.GetFirstItem("Body")
 If curDoc.HasEmbedded Then
  fileList = Evaluate(expression, curDoc)
 'Contains an array of attachmentnames
  Forall fileName In fileList
   fileType = Lcase(Strrightback(Cstr(fileName), "."))
   If (fileType = "jpeg" Or fileType = "jpg" Or fileType = "gif") 
And (Left$(fileName, 2) <> thumbnailPrefix) 
Then 'The code only works with these image types, 
and we exclude old thumbnails
    fileNameExcludingType = Strleft(fileName, ".")
    Set notesEmbeddedObject = 
curDoc.GetAttachment( fileName ) 
'We get a handle to the file that is to be the 
source of a thumbnail
    sourceFilePath = workingDir & fileName 
'The file name of the file - on disk
    thumbFilePath = workingDir & thumbnailPrefix 
&  fileNameExcludingType & "." & fileType 'The file name for 
the thumbnail
    Call notesEmbeddedObject.ExtractFile(workingDir & fileName) 
'Writing the source file to disk
    returnCode = thumbnailObject.ThumbnailThis(sourceFilePath, 
ThumbFilePath , maxX, maxY) 'Calling the 
    If returnCode = "OK" Then 'If thumbnail creation was OK
     Set notesEmbeddedObject = 
bodyItem.EmbedObject(EMBED_ATTACHMENT, "
",thumbFilePath) 'Attaching 
the thumbnail
     Call curDoc.Save(True, True, True) 
'We only save if we modify the document
    End If 'If returnCode = "OK" Then
    Kill sourceFilePath 'Deleting temporary files
    Kill thumbFilePath
   End If 'If fileType = "jpeg" Or fileType =
 "jpg" Or fileType = "gif" Then
  End Forall 'Forall fileName In fileList
 End If 'If curDoc.HasEmbedded Then
 Exit Sub
 Print " Error (" & Err & ") - line: " & Erl
 Exit Sub
End Sub

Below is the Java Library that makes it possible to create the thumbnail – when we make the call for thumbnailObject.ThumbnailThis above.

You create a Java Library in the Designer in Shared code->Script Libraries, click New Java Library, and then remove the few lines of code that you get as a help for getting started, and paste the code below. Save and call the library "ThumbNail".

import com.sun.image.codec.jpeg.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;

public class ThumbNail {
 public String thumbNailThis(String inputFilePath, 
String outputFilePath, int maxX, int maxY) {
try {
    int thumbHeight;
    int thumbWidth;
    // load source image file
    Image image = 
    MediaTracker mediaTracker = 
new MediaTracker(new Container());
    mediaTracker.addImage(image, 0);

    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);
    double imageRatio = (double)imageWidth / 
    if (imageRatio<1) {
     thumbHeight = maxY;
     thumbWidth = (int)(maxY*imageRatio);
    } else {
     thumbWidth = maxX;
     thumbHeight = (int)(maxX/imageRatio);

  } catch(Exception e) {

  return "OK";

 private void resizeImage(Image pImage, 
String pstFileName, int piWidth, int piHeight, int piQuality) {
  try {
    // draw original image to thumbnail image object and
    // scale it to the new size on-the-fly
    BufferedImage thumbImage = 
new BufferedImage(piWidth, 
piHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics2D = thumbImage.createGraphics();
    graphics2D.drawImage(pImage, 0, 0, piWidth, piHeight, null);
    // save thumbnail image to OUTFILE
    BufferedOutputStream out = 
new BufferedOutputStream(new FileOutputStream(pstFileName));
    JPEGImageEncoder encoder = 
    JPEGEncodeParam param = 

    param.setQuality((float)piQuality / 100.0f, false);
  } catch(Exception e) {


Jens Bruntt is a senior consultant at Convergens, a Danish company for whom he architects Domino-browser based Internet, intranet and extranet sites. He has been a developer and infrastructure advisor for Notes and Domino projects since 1994 and is an R6 Principal Developer. He is also's expert on Web development. Jens gives credit for this tip to Min-Chi Huang, who made sample thumbnailing code available to the project, "Open Notes Picture Database."


This is a very useful article. However, be careful about useing in WebQuerySave-Agent. Nobody knows (not even IBM) what happens. Another problem is the use of "Toolkit.getDefaultToolkit().getImage(inputFilePath);" from the java.awt.* package -- it does not work on Unix/Linux maschines without startet X-Window Server.

—Matthias G.

Do you have comments on this tip? Let us know.

Please let others know how useful this tip is via the rating scale below. Do you have a useful Notes/Domino tip or code to share? Submit it to our monthly tip contest and you could win a prize and a spot in our Hall of Fame.

This was last published in July 2005

Dig Deeper on Lotus Notes Domino Agents

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.




  • iSeries tutorials's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...