benpoole.com

“It is a very sad thing that nowadays there is so little useless information.”

ImageInfo (Win32 only)

Written by Ben Poole on 30 Apr 2003
Categories / keywords: Lotusscript, Attachments, Images
(click on a term to search for related articles at this site)

Add a comment...



Background

This code is really useful; given the full path to an image file (PNG, JPEG, GIF or BMP format), it will extract the following pieces of information about that file:

I've used this code for a few years now. I'm pretty certain it works in Notes 4.x, and it certainly works in R5 and ND6 (all on the Windows platform mind!). The code is a port of David Crowell's original CImageInfo VB routine, and works pretty well. Read the comments within the script library for some useful pointers (especially the comments with regards the default buffer size).

The attached file is a Lotusscript source document, and can be readily pasted or imported into a script library in your Notes database.

Usage

Currently, I find the code useful when attaching images to resource documents for Notes-based web content management systems. The resource / image document "knows" the image's height and width once it's attached, and therefore an HTML fragment can be put together for easy reference elsewhere in the system. An example agent calling the routine is included below.

Example calling code

Create an agent or whatever, set it to use the library, and you're set. In this example, my image is attached to a rich text field called Body, and its height / width attributes are stored in fields called txtHeight and txtWidth respectively. I've tested this specific code in Notes 6 with no issues, but it should also work in R5. Edit the workspace.OpenFileDialog line so that the file dialog starts in your preferred location — in this example we just start looking for files from the root.


Sub Initialize
	Dim workspace As New NotesUIWorkspace
	Dim uiDoc As NotesUIDocument
	Dim doc As NotesDocument
	Dim itmImg As NotesRichTextItem
	Dim strFilePath As String
	Dim varFileDlg As Variant
	Dim varProperties As Variant
	Dim object As NotesEmbeddedObject
	
	Const K_NO_FILE = "Halting: no image file selected."
	Const K_CANX = "Halting: operation cancelled by user."
	Const K_TITLE = "System Message"
	
	Set uiDoc = workspace.CurrentDocument
	Set doc = uiDoc.Document
	
' // Open a dialogbox to select the required file
	varFileDlg = workspace.OpenFileDialog(False, "Attach Image",_
	"Supported Images|*.jpg;*.bmp;*.gif;*.png", "C:\")
	
' // Exit if the user selects the Cancel button	
	If Not(Isarray(varFileDlg)) Then
		Print K_CANX
		Exit Sub
	Else
		Call uiDoc.Save()
		strFilePath = varFileDlg(0)
	End If
	
	If strFilePath = "" Then
		Messagebox K_NO_FILE, 0, K_TITLE
		Exit Sub
	End If
	
	varProperties = getImageProps(strFilePath)
	
	If uiDoc.EditMode = True Then
		uiDoc.EditMode = False
	End If
	
' // Set the document fields from the function
	doc.txtHeight = varProperties(1)
	doc.txtWidth = varProperties(2)
	
	Set itmImg = doc.GetFirstItem("Body")
	If itmImg Is Nothing Then
		Set itmImg = New NotesRichtextItem(doc, "Body")
	End If
	Set object = itmImg.EmbedObject(EMBED_ATTACHMENT,_
	"", strFilePath)
	doc.txtFileName = object.Name
	Call doc.Save(True, True)
	
' // Hack when working with RT items in back-end;
' // need to close & re-open ui doc to see attachment
	Call uiDoc.Close()
	Call workspace.EditDocument(True, doc, "",_
	"", True, False)
End Sub


Further reading:


Attachment(s): libInfo.lss (4 KB)

   » ImageInfo (Win32 only) (Ben Poole 30 Apr 2003)
   RE: ImageInfo (Win32 only) (Ben Poole 4 Mar 2004)
   RE: ImageInfo (Win32 only) (Ozlem Kuscu 29 Jul 2004)
   RE: ImageInfo (Win32 only) (Anthony 11 Dec 2006)

Add a comment...