Get Direct Pixel Access To Bitmaps In Delphi XE5 Firemonkey On Android And IOS

Delphi XE5 Firemonkey Pixel ScanlineI’m working with a developer to build a component and one of the issues we ran into is that when you take a screenshot of a set of controls with Delphi XE5 Firemonkey and then put it into a TImage control Firemonkey applies an antialias to the image. This is mainly an issue around text. Since we were looking for a pixel perfect representation of the controls as a bitmap I looked for some way to help remove the antialias effect from the bitmap. The background of the controls is transparent so one idea I came up with was to change all of the partially transparent pixels below a certain threshold to fully transparent. On the Embarcadero site I found this TestAlphaColorToPixel() function which shows changing every third line to solid red. I modified this function to read each pixel, check it’s opacity, and if it is below the opacity threshold change it to completely transparent. It does not completely solve the antialiasing problem but it helps slightly. You can take this function and modify it to suit your own needs for modifying all of the pixels in a bitmap. TAlphaColorRec has four properties you can use for this which are A for alpha, R for red, B for blue, and G for green. This function should work on Android, IOS, Windows, and OSX. You can take a look at the other PixelFormat functions including scanline access using ScanLineToAlphaColor.

If you know of a way to have Firemonkey not automatically antialias bitmaps or a better way to deantialias a bitmap let me know. I’ve also tried a Sharpen effect but that causes artifacts on edges of controls like TPanel. There are two StackOverflow answers which explain the issue here and here.

Check out the TBitmap.Map method over at Embarcadero which is the key to unlock direct access to the bitmap which making this function work.

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Get Direct Pixel Access To Bitmaps In Delphi XE5 Firemonkey On Android And IOS by Delphi XE5 XE6 XE7 XE8 10 Seattle Berlin Firemonkey, Delphi Android, Delphi IOS is licensed under a Creative Commons Attribution 4.0 International License.

3 Responses to Get Direct Pixel Access To Bitmaps In Delphi XE5 Firemonkey On Android And IOS

  1. Pingback: Speed Up Your Apps With Bitmap Caching In Delphi XE5 Firemonkey On Android And IOS | Delphi XE5 Firemonkey, Delphi Android, Delphi IOS

  2. Csaba says:

    I upgraded to Delphi 10 Seattle and FMX.PixelFormats Doesn’t work… Any idea?

    • Admin says:

      procedure TImageCacheLayout.ApplyNoAlphaEdge(ABitmap: TBitmap; OpacityThreshold: integer);
      bitdata1: TBitmapData;
      I: integer;
      J: integer;
      C: TAlphaColor;
      if (ABitmap.Map(TMapAccess.maReadWrite, bitdata1)) then
      for I := 0 to ABitmap.Width – 1 do
      for J := 0 to ABitmap.Height – 1 do
      C := PixelToAlphaColor(@PAlphaColorArray(bitdata1.Data)
      [J * (bitdata1.Pitch div PixelFormatBytes[ABitmap.PixelFormat])
      + 1 * I], ABitmap.PixelFormat);
      C := PixelToAlphaColor(@PAlphaColorArray(bitdata1.Data)
      [J * (bitdata1.Pitch div GetPixelFormatBytes(ABitmap.PixelFormat))
      + 1 * I], ABitmap.PixelFormat);
      if TAlphaColorRec(C).A

Leave a Reply

Show Buttons
Hide Buttons
Read previous post:
Delphi XE5 Firemonkey Alternate Reality 3D Map Demo
Alternate Reality Map 3D Demo For Delphi XE5 Firemonkey On Android And IOS

I put together this alternate reality 3D map demo using Delphi XE5 Firemonkey and tested it on Windows and Android...