Recent

Author Topic: Does TPicture have an alpha channel and how can I manipulate it?  (Read 1004 times)

pleumann

  • Jr. Member
  • **
  • Posts: 97
Hello,

I have a form with an Image component stretched to alClient. Its Picture property is assigned a JPG or PNG in the designer. At runtime, I would like to manipulate that Picture. I already found out that Image.Picture.Bitmap.Canvas.Pixels provides r/w access to the image data and changes are reflected on the screen immediately. What I didn't manage is to manipulate the alpha channel. I tried to and/or/xor bits 24-31, but that wouldn't change anything. Is it possible? Do I maybe need to setup the underlying bitmap in a special way? And would it also work with more efficient drawing operations by leveraging the DrawingMode?

Best regards
Joerg

wp

  • Hero Member
  • *****
  • Posts: 12577
Re: Does TPicture have an alpha channel and how can I manipulate it?
« Reply #1 on: January 28, 2024, 02:20:19 pm »
First of all: TPicture, by itself, does not have an alpha channel, it's not even a graphic format... It is just a helper class to allow the visual TImage component to handle different graphics formats.

The alpha channel is in the Bitmap property of TPicture (Image.Picture.Bitmap), but only if the bitmap has been setup for 32bits per pixel. And even then you will have a hard time manipulating the alpha channel in the LCL because TColor in the LCL does contain 4 bytes but the 4th byte is reserved for identification of system colors.

You should switch to another graphics library for manipulating alpha: BGRABitmap is a well-known candidate (the "A" stands for "alpha"), but it is an external library. If you want to use only built-in units you could take a TLazIntfImage and convert the Bitmap to a descendant of TFPCustomImage in which every pixel (Colors[x,y]) consists of four word-sized channels (.Red, .Green, .Blue, .Alpha).

Documentation about this is a bit scarce, maybe https://wiki.freepascal.org/Developing_with_Graphics, or https://wiki.freepascal.org/fcl-image.

I am attaching a little project in which a 24-bit Bitmap is loaded into a TImage with one distinct background color. And the pixels with this color are replaced by fully alpha-transparent pixels.

pleumann

  • Jr. Member
  • **
  • Posts: 97
Re: Does TPicture have an alpha channel and how can I manipulate it?
« Reply #2 on: January 31, 2024, 08:11:12 am »
Thanks! I'll have a look at the different suggestions. If neither works I suppose I could always maintain my own "alpha layer" separately and handle it in onPaint. I put it in quotes because in the end what I need is to hide/show parts of my bitmap and draw something else instead, but maybe with a glimpse of the actual bitmap. That could be done by putting that something below the bitmap and using alpha, but I could also draw something on top that has the "opposite" alpha.

 

TinyPortal © 2005-2018