B4J Library [B4X] [XUI] xResizeAndCrop

The xResizeAndCrop Class does now also exist as a B4X libary.
It works on all three platforms: B4A, B4i and B4J.
The xResizeAndCrop.b4xlib and the xResizeAndCrop.xml files are attached.
You need to copy the xResizeAndCrop.b4xlib file to the AdditionlLibraries\B4X folder!
Don’t copy thexResizeAndCrop.xml file to the AdditionalLibraries folder, copy it in another folder for all b4xlib xml files.
Example: AdditionlLibraries\B4XlibXMLFiles
The xResizeAndCrop.xml file is for help purposes and is useful with the B4X Help Viewer or the B4XObjectBrowser.
The xResizeAndCrop.xml was generated with this tool: b4xlib - XML generation

This is a cross-platform CustomView.
It allows to resize and crop images.
It works on all three platforms: B4A, B4i and B4J.

It is an evolution of the B4A project: Resize and crop image.
To make a B4X CustomView was inspired by this thread: [B4X] [XUI] CropView.

Demoprogramms for all three platforms.
Tested on PC, Android Samsung S6, iPhone 6.

EDIT: 2020.07.15 Version 1.4
Amended error with CroppedImage without a CroppedView.

EDIT: 2020.06.25 Version 1.3
Updated the Tag property according to Erels recommendation:

EDIT: 2019.03.12 Version 1.2
Added WidthHeightRatio and WidthHeightRatioValue properties
defining fixed or custom Width / Height ratios for the cropped image
Added RotateImage method, original image rotation.

EDIT: 2019.01.30 Version 1.1
Added a Square property.
When Square = True then the resized image is always square.



Klaus CHRISTL (klaus)
Version: 1.4
  • xResizeAndCrop
    • Events:
      • CropFinished
    • Functions:
      • Initialize (Callback As Object, EventName As String) As String
      • IsInitialized As Boolean
        Tests whether the object has been initialized.
      • LoadImage (Dir As String, FileName As String) As String
        loads the given image file to the xResizeAndCrop Customview
      • RotateImage(Degrees As Int)
    • Properties:
      • CroppedImage As B4XBitmap [read only]
        gets the cropped image as a B4XBitmap, read only
      • CroppedView
        sets the Panel / Pane view for the cropped image, write only
        the Panel / Pane view must be added in the main code and transmitted to the custom view as this property.
      • HandleColor As Int
        sets or gets the HandleColor property
        value must a xui color
        Example code: <code>xResizeAndCrop1.HandleColor = xui.Color_RGB(255, 215, 0)</code>
      • Height As Int
        sets or gets the Height property
      • Image As B4XBitmap
        sets the image (B4XBitmap) to the Customview, write only
      • Left As Int
        sets or gets the Left property
      • MinCroppedHeight As Int
        sets or gets the MinHeight property
        value in pixels, no dip value
        if Square = True, the min cropped width is set equal to this height value
      • MinCroppedWidth As Int
        sets or gets the MinWidth property
        value in pixels, no dip value
        if Square = True, the min cropped height is set equal to this width value
      • Top As Int
        sets or gets the Top property
      • Visible As Boolean
        sets or gets the Visible property
      • Width As Int
        sets or gets the Width property
      • WidthHeightRatio As String
        sets or gets the WidthHeightRatio property
        possible values: None Square 3/2 2/3 4/3 3/4 16/9 9/16 Custom
        None = any width and height
        Square = True means that the resized image is always square
        3/2 = ratio 1.5
        2/3 = ratio 0.6667
        4/3 = ratio 4/3
        3/4 = ratio 3/4
        16/9 = ratio 16/9
        9/16 = ratio 9/16
        Custom = user defined ratio set in the WidthHeightRatioValue property
      • WidthHeightRatio As Double
        sets or gets the WidthHeightRatioValue property
        only active if the WidthHeightRatio property is set to "Custom"


  • xResizeAndCrop.b4xlib
    5.4 KB · Views: 186
  • xResizeAndCrop.xml
    5.7 KB · Views: 189
  • xResizeAndCropDemo_V1_4.zip
    354.7 KB · Views: 76
Last edited:


Licensed User
To get the cropped image i need a cropped panel, but why?
You don't need a Panel nor Pane!
The CroppedImage method returns a B4XBitmap.
Don't confuse with CroppedView!
This a B4XView where you can automatically copy the cropped image.

Alexander Stolte

Licensed User
ou don't need a Panel nor Pane!
but on the code i see this:
Private Sub ExtractCroppedImage
    Private rectDest As B4XRect
    Private cLeft, cTop, cWidth, cHeight As Int
    If xpnlCropped.IsInitialized Then
        If rectImage.Width / rectImage.Height > imvCroppedWidth / imvCroppedHeight Then
            xpnlCropped.Width = imvCroppedWidth
            xpnlCropped.Height = imvCroppedWidth / rectImage.Width * rectImage.Height
            xpnlCropped.Height = imvCroppedHeight
            xpnlCropped.Width = imvCroppedHeight / rectImage.Height * rectImage.Width
        End If
        xcvsCropped.Resize(xpnlCropped.Width, xpnlCropped.Height)
        cLeft = Floor((rectImage.Left - ximvImage.Left) * PixelScale)
        cTop = Floor((rectImage.Top - ximvImage.Top) * PixelScale)
        cWidth = Ceil(rectImage.Width * PixelScale)
        cHeight = Ceil(rectImage.Height * PixelScale)
        xbmpCropped = xbmpImage.Crop(cLeft, cTop, cWidth, cHeight)
        rectDest.Initialize(0, 0, xpnlCropped.Width, xpnlCropped.Height)
        xcvsCropped.DrawBitmap(xbmpCropped, rectDest)
    End If
End Sub
and this panel is set on the setCroppedView

if i call CroppedImage without to set a "CroppedView" then the "CoppedImage" is null.
or am I looking at this wrong?


Licensed User
You are right, there was a problem.
The xResizeAnd Crop Class has been updated to version 1.4 in the first post.
Last edited:


Well-Known Member
Licensed User
Hello Klaus,

I tried your code yesterday (it works fine), but today there is the following message:
[IDE message - 1:52:41]
Ein Fehler trat auf.
Fehlende jar-Datei für die Bibliothek: xResizeAndCrop.xml
... and the message apears also when I open B4A with all my other codes!
What is the reason for that?


Licensed User
Remove the xResizeAndCrop.xml file from your AdditionalLibraries folder as written in the thread:
Don’t copy thexResizeAndCrop.xml file to the AdditionalLibraries folder, copy it in another folder for all b4xlib xml files.
The xResizeAndCrop.xml is only needed if you use the B4X Help Viewer.


Well-Known Member
Licensed User
Oooh sorry Klaus, that was my mistake!
I've already read the sentence and copied the xml file into an extra folder, but at the same time I copied this file into the library folder ... :oops: