Skip to main content

Saving Image Snippets in Validation Using a Lasso


QAID # 17769 Published

Question / Problem:

Saving Image Snippets in Validation Using a Lasso

Answer / Solution:

(New in KTM 5.0 Patch 4)

Use Case

You can use the mouse to lasso around an area in the image viewer in Validation and then catch an event that gives you the coordinates of the lassoed area. These coordinates can be used to cut a snippet from the image and save it to disk. Additionally you can cancel the default behaviour of the lasso, which is to add the lassoed data to the current field.

To do this, we have introduced a new Validation form event AfterViewerLassoDrawn.


The best way to understand how to do this is an example. The project for this example can be downloaded from this link: Lasso Event sample project

The example is based on:


Screen Shot 2018-09-12 at 8.13.20 AM.png

  • SampleField Regular TextBox field with a standard KTM behavior.
  • CropFieldWhen focus is on this field, you can draw a lasso over a part of the image you would like to crop. Using a script, the lasso event will be used to save the snippet on disk, and the value of the field will be set on a snippet path.

Validation Form

Screen Shot 2018-09-12 at 8.13.55 AM.png

  • Showing the path to the saved signature snippet and also the snippet itself.


Private Sub ValidationForm_AfterViewerLassoDrawn(ByRef pXDoc As CASCADELib.CscXDocument, _
ByVal PageIndex As Long, _
ByRef pField As CASCADELib.CscXDocField, _
ByVal TopPos As Long, _
ByVal LeftPos As Long, ByVal Width As Long, _
ByVal Height As Long, ByRef bCancel As Boolean)
'Get displayed image
Dim img As CscImage
img = pXDoc.CDoc.Pages.ItemByIndex(PageIndex).GetImage
'path where snippet is saved
Dim sSavedImage As String
Select Case pField.Name
Case "CropField"
'Call the function that will save the selected laso area and return the file name
sSavedImage = CropImage(img, LeftPos, TopPos, Width, Height, bCancel)
'If the croped area is sucessfully saved update the field
If bCancel = True Then
'Set the correct field coordinates so that miniviewer shows
' the same snippet as the saved one:
pField.Width = Width
pField.Top = TopPos
pField.Left = LeftPos
pField.Height = Height
pField.PageIndex = PageIndex
'Set the field value to the filename containing the snippet and make it valid
pField.Text = sSavedImage
pField.Valid = True
End If
End Select
End Sub
Private Function CropImage(ByVal img As CscImage, ByVal LeftPos As Integer, _
ByVal TopPos As Integer, ByVal Width As Integer, _
ByVal Height As Integer, ByRef bCancel) As String
On Error GoTo Err
Dim snippet As New CscImage
Dim bRet As Boolean
'Create an image of the same size as the croped image area
snippet.CreateImage(CscImgColFormatBinary, Width, Height, img.XResolution, img.YResolution)
'Copy the croped rectangle to the newly created image
snippet.CopyRect(img, LeftPos, TopPos, 0, 0, Width, Height)
'Create the filename based on the temp folder and current date-time
Dim sFilename As String
sFilename = Environ("Temp")
sFilename = sFilename & "\" & NormalizeNow() & ".tif"
'Save the image
snippet.Save(sFilename, CscImgFileFormatTIFFFaxG4)
bCancel = True
'Open the image
Dim objShell
objShell = CreateObject("Shell.Application")
objShell.ShellExecute(sFilename, "", "", "open", 1)
objShell = Nothing
'return the saved image path
CropImage = sFilename
Exit Function
bCancel = False
End Function
Private Function NormalizeNow()
Dim sNow As String
sNow = CStr(Now)
sNow = Replace(sNow, " ", "")
sNow = Replace(sNow, ".", "")
sNow = Replace(sNow, ":", "")
sNow = Replace(sNow, "/", "")
sNow = Replace(sNow, "-", "")
NormalizeNow = sNow
End Function

Applies to:

Product Version Category
AXPRO 5.5 Project Builder