Get the pixels of an image

To put an image in a pdf file, I need to get the individual pixel colors. WEBLIB.graphics provides this property for a canvas. So, this would be easy to do. However, there are several issues:

  • The picture is a png file with transparency. All transparent pixels are shown in black, when accessing the pixels. Is there a way to find out whether a black pixel is really black or black because of transparency?
  • When getting the pixels from a TGraphic, they are all black. To get the pixels from an image, I first have to draw the TGraphic on a TBitmap canvas and get the pixels from there.
  • The definition of "Pixels" is different in Vcl.graphics (defined in TCustomCanvas) and using "Single" as indexes) and WEBLib.graphics (defined in TCanvas and using "Integer" as indexes).

Attached a screenshot of different copies the same image:


Picture 1: original picture loaded at design time. Transparency is correctly interpreted.
Picture 2: copied from the original through drawing a TGraphic on a paintbox: correct
Picture 3: copied drawing the TGraphic on a TBitmap. Pixel colors are available and editable, but transparency lost.
Picture 4: accessing pixels from a TGraphic returns black pixels.

Attached the program demonstrating this behavior.
Transparent.zip (22.9 KB)

The biggest problem is the loss of transparency, as pictures in the real application come from a server or from a database (blob). All of them are transparent and must appear in a pdf report.

Thank you for looking into this.

Kind regards,

Michel Huybrechts
Micriconsult BV

We have applied an improvement where the alpha channel will also be returned in the pixel data.
You can then test in the following way if the pixel is transparent:

        OneColor := TheBitmap.Canvas.Pixels [i, j];
        if (OneColor and $FF000000) = $00000000 then
        //    this color is transparent

This enhancement will be included in the next update.

Dear Sir,

This works indeed. Thank you for this fast answer. Will this solution still work in the next update, after the pixels property is extended?

The problem with getting the pixels straight from the canvas of a TGraphic remains: it gives 0 for all pixels, but setting pixels in a TGraphic canvas works.

Kind regards,

Michel Huybrechts

Micriconsult BV

www.micriconsult.be

This solution will still work with the extension done.
Getting out of the TGraphic directly is not possible.

Dear Sir,

Thank you for this information. Everything works and is stable.

I was not too surprised nothing came out of the TGraophic. It is rather the other way around: I can fill up a TGraphic pixel per pixel. And the pixels property differs between WEBLib.Graphics and Vcl.Graphics, but this has no influence on the operation.

Kind regards,

Michel Huybrechts

Micriconsult BV

www.micriconsult.be