IBM®
Skip to main content
    Country/region select      Terms of use
 
 
   
     Home      Products      Services & solutions      Support & downloads      My account     

developerWorks  >  Lotus  >  Forums & community  >  Best Practice Makes Perfect

Best Practice Makes Perfect

A collaboration with Domino developers about how to do it and how to get it right in Domino

Prompted by a question in the forums, I searched around and didn't find a good example of this technique, so here's a sample of confirming that a rich text field contains exactly one image, with restrictions on the dimensions.

Sub Querysave(Source As Notesuidocument, Continue As Variant)
        Dim doc As NotesDocument
        On Error Goto oops
        Source.Refresh True ' update rich text data into back end.
        Set doc = Source.Document
        Dim session As New NotesSession
        Dim stream As NotesStream
        Dim dxle As NotesDXLExporter
        Dim domp As NotesDOMParser
        Dim domd As NotesDOMDocumentNode
        Dim nl As NotesDOMNodeList
        Set stream = session.CreateStream
       
        Set dxle = session.CreateDXLExporter(doc, stream)
        dxle.RestrictToItemNames = "Body"
        ' at this point you might want to set additional properties for efficiency, e.g. to omit attachments
        ' please note that if you want to disallow certain types of content, you can use a Rich Text Lite field,
        ' so this is not the preferrred way to block users from adding an image.
        dxle.Process
        Set domp = session.CreateDOMParser(stream)
        domp.Parse
        Set domd = domp.Document
        Set nl = domd.GetElementsByTagName("picture")
        If nl.NumberOfEntries <> 1 Then
                Msgbox "You must have exactly one image in the Body field."
                Continue = False
                Exit Sub
        End If
        Dim picture As NotesDOMElementNode
        Set picture = nl.GetItem(1)
        Dim picX As Long, picY As Long
        picX = Clng("0"+Strleft(picture.GetAttribute("width"), "px"))
        picY = Clng("0"+Strleft(picture.GetAttribute("height"), "px"))
        If picX > 400 Or picY > 400 Then
                Msgbox "The image may not exceed 400x400 pixels."
                Continue = False
                Exit Sub
        End If
        If stream.Bytes > 160000 Then
                Msgbox "The image is too large. Please IMPORT the image from a GIF or JPEG file; do not paste graphics or import a BMP file. Thanks."
                Continue = False
                Exit Sub
        End If
        ' else: success!
        Exit Sub
oops:
        ' if a validation failure don't bother to display the error because user already saw one.
        If Err <> 4412 Then Msgbox "Error " & Err & " in Querysave line " & Erl & ": " & Error
        Continue = False
        Exit Sub
End Sub


Please note that the width and height properties refer to the dimensions before scaling; if the user drags the picture bigger or smaller these values will not change, but scaling values will be added to the DXL, so you might need to look at those in addition/instead.

I've done a crude size check by checking stream.Bytes; the user could exceed this limit by other things than having a too-large graphic. For instance, if the field contained a lot of text in addition to the image. To more precisely check the image size, you would have to check the length of the base64-encoded character node which is a second-level descendant of the element. E.g. you might see:

...
R0lGODlhCAAIAKIAAP///9HHx8m+vpiHh4p4eFQAADoDAy4BASH5BAkIAAAALAAAAAAIAAgAAAMa
CEqkLueIZ6qZ5JhSzGEa5zWCdT3R5ACMkwAAOw==
...

has a child (or or ) and the image data is a child of that node.

Andre Guirard | 9 September 2009 09:25:56 AM ET | Home, Plymouth, MN, USA | Comments (5)


 Comments

1) Nice...
Morten Clausen | 9/10/2009 6:17:24 AM

Regulating pictures is of great interest. It's one of the many ways databases can start growing uncontrollably and it's very hard to police after the fact - getting users to go back and shrink pictures for real is in my experience very, very hard.

2) wonders of DXL
daniel barros | 9/10/2009 6:44:21 AM

I've been very pleased with the things I'm learning with dxl, this is a good example.

3) Not working with Rich Text Lite field
Rashid Azar | 9/14/2010 9:51:51 AM

This code is not working with rich text lite field.

Set nl = domd.GetElementsByTagName("picture")

In the above statement, it returns nothing as there is no element with name "picture" in rich text lite field.

4) NOT WORKING!!! UPDATED NEEDED
Iwan | 9/13/2016 8:44:22 AM

As Rashid mentioned, this code does not work, please could you update or remove this article as it's causing confusion, thanks

5) Could you please be more specific
Andre Guirard | 9/16/2016 4:00:31 PM

...as to Notes version and exactly how you inserted the image and what sort of image it was? It works for me.

6) Awsome!
Hester | 10/11/2016 9:29:55 AM

Thank you ! I used it in my forms Query save and it does exactly what is needed

 Add a Comment
Subject:
   
Name:
Comment:  (No HTML - Links will be converted if prefixed http://)
 
Remember Me?     Cancel

Search this blog 

Disclaimer 

    About IBM Privacy Contact