AutoTrim example for Visual Basic

'This example scans a document and trims the edges.

'Declare the Raster objects WithEvents
Private WithEvents RasterTwain As LEADRasterTwain
Private WithEvents RasterIO As LEADRasterIO
Private WithEvents RasterProc As LEADRasterProcess

‘Create the Raster objects
Private Sub Form_Load()
    Dim bRet As Boolean
    Set RasterIO = CreateObject("LEADRasterIO.LEADRasterIO. ")
    Set RasterTwain = CreateObject("LEADRasterTwain.LEADRasterTwain. ")
    Set RasterProc = CreateObject("LEADRasterProcess.LEADRasterProcess. ")

   ‘Unlock support—substitute the unlock support key for L_KEY_DOCUMENT
    LEADRasterView1.Raster.UnlockSupport L_SUPPORT_DOCUMENT, L_KEY_DOCUMENT


End Sub

Dim SavedSetting As Integer
Dim CalcHeight As Double
Dim nRet As Integer

'Make sure display rectangles are reset when we acquire the image.
LEADRasterView1.AutoSetRects = True

'Disable automatic repainting.
LEADRasterView1.AutoRepaint = False

'Acquire the image, allowing for possible mistakes.
SavedSetting = LEADRasterView1.EnableMethodErrors
RasterTwain.EnableMethodErrors = False
nRet = RasterTwain.Acquire(L_LTWAIN_SHOW_USER_INTERFACE)
If nRet <> 0 Then
    MsgBox "TWAIN device was not ready"
    RasterTwain.EnableMethodErrors = SavedSetting
    GoTo FINISHED
End If
RasterTwain.EnableMethodErrors = SavedSetting

'Trim the edges of the acquired bitmap.
RasterProc.AutoTrim LEADRasterView1.Raster, 64

'Use the whole window.
LEADRasterView1.Top = 0
LEADRasterView1.Left = 0
LEADRasterView1.Width = ScaleWidth
LEADRasterView1.Height = ScaleHeight

'Set up for good display of a typical scanned document.
LEADRasterView1.BitonalScaling = BITONALSCALING_SCALETOGRAY
LEADRasterView1.AutoScroll = True
LEADRasterView1.DstWidth = LEADRasterView1.Raster.ScaleWidth 'Use the full control width.
LEADRasterView1.DstClipWidth = LEADRasterView1.Raster.ScaleWidth
CalcHeight = (LEADRasterView1.Raster.BitmapHeight * LEADRasterView1.Raster.ScaleWidth) / LEADRasterView1.Raster.BitmapWidth
LEADRasterView1.DstHeight = CalcHeight 'Use the proportional height.
LEADRasterView1.DstClipHeight = CalcHeight

'Enable automatic repainting.
LEADRasterView1.AutoRepaint = True
FINISHED: