CropEffectLib — Image Crop Effect

Crops a rectangular region from the texture of a visual control using pixel coordinates. The cropped area is then scaled to fill the control’s display bounds. Wraps FireMonkey’s TCropEffect. Useful for image viewports, zoom previews, sprite sheet extraction, and interactive region selection. 18 functions.

CategoryCountDescription
Error Handling4crop_error, errormsg$, strerror$, clearerror
Creation & Destruction2crop# (create), crop_free (destroy)
Crop Region8lefttopx, lefttopy, rightbottomx, rightbottomy (get/set)
Effect Control4enabled, trigger (get/set)
⚠ Images Only: This effect works on bitmap/texture data only. It does not work on vector shapes like rectangles or circles. Use with TImage controls loaded via image_load#.

Coordinate System

The crop region is defined by four pixel coordinates relative to the original image dimensions, not the control’s display size. The top-left corner of the image is (0, 0).

╯ coordinate-diagram
  (0,0)                        (200,0)
    +------------------------------+
    |                              |
    |    (LeftTopX, LeftTopY)      |
    |       +------------+         |
    |       |  CROP AREA |         |
    |       |  (visible) |         |
    |       +------------+         |
    |       (RightBottomX, RightBottomY)
    |                              |
    +------------------------------+
  (0,150)                      (200,150)
PropertyDefaultDescription
LeftTopX0Left edge of crop region (pixels from left)
LeftTopY0Top edge of crop region (pixels from top)
RightBottomX10000Right edge of crop region (pixels from left)
RightBottomY10000Bottom edge of crop region (pixels from top)
ⓘ Default = Full Image: With defaults of 0, 0, 10000, 10000 the crop region extends far beyond any practical image size, so the entire image is shown. Set specific coordinates to define the actual crop area.

Cross-Platform Support

PlatformStatusNotes
Windows✅ Full SupportGPU-accelerated via Direct2D
Linux✅ Full SupportSoftware rendering fallback
Android✅ Full SupportGPU-accelerated

Error Handling

FunctionSignatureDescription
crop_error()crop_error@Returns last error code (0 = no error)
crop_errormsg$()crop_errormsg$@Returns last error message as string
crop_strerror$(code)crop_strerror$@nConverts an error code to descriptive text
crop_clearerror()crop_clearerror@Clears the error state

Error Codes

CodeConstantMeaning
0ERR_NONENo error
1ERR_NIL_EFFECTEffect pointer is nil
2ERR_INVALID_EFFECTPointer is not a valid TCropEffect
3ERR_INVALID_VALUEProperty value out of range
4ERR_NIL_PARENTParent control pointer is nil
5ERR_INVALID_PARENTPointer is not a valid TFmxObject

Creation & Destruction

FunctionSignatureDescription
crop#(parent#)crop#@#Creates a crop effect attached to the given control. Returns the effect pointer.
crop_free(effect#)crop_free@#Destroys the effect and removes it from the parent control.

Crop Region

Four coordinate functions define the rectangular crop area in pixel units relative to the original image.

FunctionSignatureDescription
crop_lefttopx#(effect#, value)crop_lefttopx#@#nSets left edge X (pixels)
crop_lefttopx(effect#)crop_lefttopx@#Gets left edge X
crop_lefttopy#(effect#, value)crop_lefttopy#@#nSets top edge Y (pixels)
crop_lefttopy(effect#)crop_lefttopy@#Gets top edge Y
crop_rightbottomx#(effect#, value)crop_rightbottomx#@#nSets right edge X (pixels)
crop_rightbottomx(effect#)crop_rightbottomx@#Gets right edge X
crop_rightbottomy#(effect#, value)crop_rightbottomy#@#nSets bottom edge Y (pixels)
crop_rightbottomy(effect#)crop_rightbottomy@#Gets bottom edge Y

Common Crop Patterns (200×150 image)

CropLeftTopXLeftTopYRightBottomXRightBottomY
Full image00200150
Left half00100150
Right half1000200150
Top half0020075
Center 50%5037150112
Top-left quadrant0010075
Bottom-right quadrant10075200150
╯ crop-region.bas
' Crop to center 50% of a 200x150 image
crop_lefttopx#(crp#, 50)
crop_lefttopy#(crp#, 37)
crop_rightbottomx#(crp#, 150)
crop_rightbottomy#(crp#, 112)
crop_enabled#(crp#, 1)

Effect Control

Enabled

FunctionSignatureDescription
crop_enabled#(effect#, value)crop_enabled#@#nEnables (1) or disables (0)
crop_enabled(effect#)crop_enabled@#Gets enabled state

Trigger

FunctionSignatureDescription
crop_trigger#(effect#, trigger$)crop_trigger#@#$Sets trigger string
crop_trigger$(effect#)crop_trigger$@#Gets current trigger string

Complete Examples

Basic Crop with Presets

╯ crop-basic.bas
let frm# = Pointer#(0)
let img# = Pointer#(0)
let crp# = Pointer#(0)

frm# = form#("Crop Demo", 400, 350)

' Image is 200x150 pixels
img# = image#(frm#)
image_bounds#(img#, 100, 30, 200, 150)
image_load#(img#, "https://picsum.photos/200/150")

crp# = crop#(img#)
crop_enabled#(crp#, 0)  ' Start disabled until image loads

let btn1# = button#(frm#, "Crop Left Half")
button_bounds#(btn1#, 50, 210, 120, 30)
button_onclick#(btn1#, "CropLeft")

let btn2# = button#(frm#, "Crop Center")
button_bounds#(btn2#, 180, 210, 120, 30)
button_onclick#(btn2#, "CropCenter")

let btn3# = button#(frm#, "Reset")
button_bounds#(btn3#, 115, 250, 120, 30)
button_onclick#(btn3#, "CropReset")

form_show(frm#)

function CropLeft(sender#)
  crop_lefttopx#(crp#, 0)
  crop_lefttopy#(crp#, 0)
  crop_rightbottomx#(crp#, 100)
  crop_rightbottomy#(crp#, 150)
  crop_enabled#(crp#, 1)
endfunction

function CropCenter(sender#)
  crop_lefttopx#(crp#, 50)
  crop_lefttopy#(crp#, 37)
  crop_rightbottomx#(crp#, 150)
  crop_rightbottomy#(crp#, 112)
  crop_enabled#(crp#, 1)
endfunction

function CropReset(sender#)
  crop_enabled#(crp#, 0)
endfunction

Quadrant Selector

Five buttons to view the full image or any of its four quadrants.

╯ crop-quadrants.bas
let frm# = Pointer#(0)
let img# = Pointer#(0)
let crp# = Pointer#(0)
let lbl# = Pointer#(0)

frm# = form#("Quadrant Crop", 450, 380)

img# = image#(frm#)
image_bounds#(img#, 125, 20, 200, 150)
image_load#(img#, "https://picsum.photos/200/150")

crp# = crop#(img#)
crop_enabled#(crp#, 0)

lbl# = label#(frm#, "Click button after image loads", 115, 185)

let btn1# = button#(frm#, "Full")
button_bounds#(btn1#, 40, 220, 80, 30)
button_onclick#(btn1#, "SetFull")

let btn2# = button#(frm#, "Top-Left")
button_bounds#(btn2#, 130, 220, 80, 30)
button_onclick#(btn2#, "SetTL")

let btn3# = button#(frm#, "Top-Right")
button_bounds#(btn3#, 220, 220, 80, 30)
button_onclick#(btn3#, "SetTR")

let btn4# = button#(frm#, "Bottom-L")
button_bounds#(btn4#, 130, 260, 80, 30)
button_onclick#(btn4#, "SetBL")

let btn5# = button#(frm#, "Bottom-R")
button_bounds#(btn5#, 220, 260, 80, 30)
button_onclick#(btn5#, "SetBR")

form_show(frm#)

function SetFull(sender#)
  crop_enabled#(crp#, 0)
  label_text#(lbl#, "Showing: Full Image")
endfunction

function SetTL(sender#)
  crop_lefttopx#(crp#, 0)
  crop_lefttopy#(crp#, 0)
  crop_rightbottomx#(crp#, 100)
  crop_rightbottomy#(crp#, 75)
  crop_enabled#(crp#, 1)
  label_text#(lbl#, "Showing: Top-Left Quadrant")
endfunction

function SetTR(sender#)
  crop_lefttopx#(crp#, 100)
  crop_lefttopy#(crp#, 0)
  crop_rightbottomx#(crp#, 200)
  crop_rightbottomy#(crp#, 75)
  crop_enabled#(crp#, 1)
  label_text#(lbl#, "Showing: Top-Right Quadrant")
endfunction

function SetBL(sender#)
  crop_lefttopx#(crp#, 0)
  crop_lefttopy#(crp#, 75)
  crop_rightbottomx#(crp#, 100)
  crop_rightbottomy#(crp#, 150)
  crop_enabled#(crp#, 1)
  label_text#(lbl#, "Showing: Bottom-Left Quadrant")
endfunction

function SetBR(sender#)
  crop_lefttopx#(crp#, 100)
  crop_lefttopy#(crp#, 75)
  crop_rightbottomx#(crp#, 200)
  crop_rightbottomy#(crp#, 150)
  crop_enabled#(crp#, 1)
  label_text#(lbl#, "Showing: Bottom-Right Quadrant")
endfunction

Best Practices

PracticeWhy
Coordinates are in pixels, not percentagesYou must know the original image dimensions to set correct crop values
Keep effect disabled until image loadsCropping before the texture exists may produce no result
Disable to show full image (instead of resetting coords)crop_enabled#(crp#, 0) instantly shows the full image
Cropped area scales to fill the controlA small crop region will be upscaled — expect lower apparent resolution
Animate crop coordinates for pan/zoom effectsUse FloatAnimationLib to smoothly pan across an image
Use for sprite sheet extractionSet crop region to a single sprite frame and swap coordinates per frame
Combine with MagnifyEffectLib for magnifying glassCrop to region + magnify = interactive zoom viewer
Ensure RightBottomX > LeftTopX and RightBottomY > LeftTopYInverted coordinates produce undefined behavior

Quick Reference

FunctionSignatureDescription
ERROR HANDLING
crop_error()crop_error@Last error code
crop_errormsg$()crop_errormsg$@Last error message
crop_strerror$(code)crop_strerror$@nError code to text
crop_clearerror()crop_clearerror@Clear error state
CREATION & DESTRUCTION
crop#(parent#)crop#@#Create effect on control
crop_free(effect#)crop_free@#Destroy effect
CROP REGION
crop_lefttopx#(effect#, px)crop_lefttopx#@#nSet left edge
crop_lefttopx(effect#)crop_lefttopx@#Get left edge
crop_lefttopy#(effect#, px)crop_lefttopy#@#nSet top edge
crop_lefttopy(effect#)crop_lefttopy@#Get top edge
crop_rightbottomx#(effect#, px)crop_rightbottomx#@#nSet right edge
crop_rightbottomx(effect#)crop_rightbottomx@#Get right edge
crop_rightbottomy#(effect#, px)crop_rightbottomy#@#nSet bottom edge
crop_rightbottomy(effect#)crop_rightbottomy@#Get bottom edge
EFFECT CONTROL
crop_enabled#(effect#, val)crop_enabled#@#nEnable (1) / disable (0)
crop_enabled(effect#)crop_enabled@#Get enabled state
crop_trigger#(effect#, str$)crop_trigger#@#$Set trigger string
crop_trigger$(effect#)crop_trigger$@#Get trigger string

18 functions. Part of the Plan9Basic visual effects library system.

See Also

  • MagnifyEffectLib — Zoom into areas with magnification lens
  • SmoothMagnifyEffectLib — Smooth magnification
  • PixelateEffectLib — Pixelation effect
  • FloatAnimationLib — Animate crop coordinates for pan/zoom