PreviousNext…

ImageInfo (Win32 only)

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:

  • File type
  • Width
  • Height
  • Colour depth

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

Downloads

Comments

  1. Read this weblog entry for a patch to this code should you experience weird dimensions for some images:

    http://benpoole.com/weblog/200403041706Ben Poole#
  2. First of all great thanks for this web site and the codes that you have shared! I used the following code and worked like charm. This code is fine for inserting an attachment to a rich text field. I am using Notes 6.5 and trying to modify an image resource data. There is $ImageData field which keeps the image data. I tried using EMBED_OBJECTLINK, EMBED_ATTACHMENT and EMBED_OBJECT. But they don't work for that situation. When I check with NotesPeek the data structure is different when I embed them to $ImageData field. I was wondering if any of you guys tried something like that or have any tips for modifying an image resource. Thanks a lot in advance.Ozlem Kuscu#
  3. This is great, implimented in less than 5 minutes. Thank you and great workAnthony#

Comments on this post are now closed.

About

I’m a software architect / developer / general IT wrangler specialising in web, mobile web and middleware using things like node.js, Java, C#, PHP, HTML5 and more.

Best described as a simpleton, but kindly. You can read more here.

";
File Attachment Icon
libInfo.lss