B4J Library [B4X] [XUI] BitmapCreator - Pixels, Drawings and More

Not open for further replies.
BitmapCreator is a cross platform class.
Its core is made of:

1. A bytes array that represents an image.
2. Platform specific code that efficiently creates a regular bitmap from the bytes array data (Bitmap property).
3. Platform specific code that efficiently extracts the pixels data from a bitmap and copies them to the bytes array (CopyPixelsFromBitmap).

Reading and writing to an array of bytes are very quick operations. This allows low level access that was previously difficult to implement and more or less impossible to implement with cross platform code.

Video example:


Additional methods in BitmapCreator:

- Methods to get or set a pixel color in various formats.
- Methods to copy a pixel from a different BitmapCreator to this BitmapCreator.
- Methods to draw a bitmap or a different BitmapCreator to this BitmapCreator.
- Method to draw gradients.
- And a few other methods.


1. The bitmap created with the Bitmap property has a scale of 1. This means that on Android the OS will resize the bitmap automatically based on the device scale.
Don't use 'dip' units with BitmapCreator.
Note that in the example class the "missing dip" warning is disabled with:
#IgnoreWarnings: 6

2. Depending on your use case it might be better from performance perspective to create a smaller BitmapCreator and resize the output Bitmap with Bitmap.Resize.


When a non-opaque pixel (source) is set, the target pixel color should be combined with the source color for the transparency to have its expected effect. This is called blending.
When you call DrawBitmap or DrawBitmapCreator you can choose whether to skip blending or not. It is significantly faster to skip blending, though drawing with blending will be fast enough in most cases.

There is also a BlendPixel method that you can use to blend a single pixel.

BitmapCreator vs. Canvas / B4XCanvas

Use them together. BitmapCreator returns a regular bitmap and Canvas can be used to make a drawing that is then copied or drawn over a BitmapCreator.


Many of the methods that you will use with BitmapCreator are computational intensive. The debugger might be too slow, this is especially true if the code is in a resumable sub as the debugger cannot optimize such subs.
As BitmapCreator is cross platform you can write the code in B4J and only test it on the other platforms.
The example project features are implemented in a class named BitmapCreatorExamples. The three projects all link to the exact same class.


B4A / B4J - XUI, JavaObject and ByteConverter (https://www.b4x.com/android/forum/threads/6787/#content)
B4i - iXUI, iRandomAccessFile

BitmapCreator v3.01 (beta) is included in the attached projects.
Note that BitmapCreator and BitmapCreatorExamples classes are identical in all three projects.
Images source: https://www.pexels.com/


Starting from B4J v6.30, B4i v5.0 and B4A v8.3, BitmapCreator is included as an internal library.
The source code is attached separately. Note that it will not work with older versions of B4J, B4i and B4A.

BitmapCreatorEffects class: https://www.b4x.com/android/forum/threads/b4x-bitmapcreator-effects.93608/


  • B4i_BitmapCreator.zip
    341.5 KB · Views: 1,271
  • B4J_BitmapCreator.zip
    341.4 KB · Views: 1,712
  • B4A_BitmapCreator.zip
    343.9 KB · Views: 1,818
  • BitmapCreator Source.zip
    24.1 KB · Views: 1,115
Last edited:
Not open for further replies.