Jump to content
peter2005

32bit bitmap

Recommended Posts

hi,

 

for unclear reason delphi does not completely support 32 bit bitmaps. I'm trying to display a 32bit picture data(with alpha channel) in a TImage using TBitmap and have not found a proper way, some parts of the picture are just black. Interesting is that the image although not properly displayed can be saved into a file(image1.Picture.Bitmap.SaveToFile) and works perfectly fine in a picture viewer.

 

Alternative is to use PNG which uses alpha channel however it complicates whole situation since TBitmap is easy to use.

 

I do not want use 3rd-part components or huge libraries  ....a code code snippet or an unit to include would be great.

Share this post


Link to post
37 minutes ago, peter2005 said:

for unclear reason delphi does not completely support 32 bit bitmaps.

Because Windows doesn't have good support for 32bit alpha-channeled bitmaps to begin with.

Quote

I'm trying to display a 32bit picture data(with alpha channel) in a TImage using TBitmap

Good luck with that.

Quote

Alternative is to use PNG which uses alpha channel however it complicates whole situation since TBitmap is easy to use.

What complications does PNG present to you?  Are you working with VCL or FMX?  The VCL's TPNGImage class is easy to use.  FMX's TBitmap can be a bit difficult, depending on what you are trying to do with it.  It would really help if you would explain the problem you are having exactly.

Edited by Remy Lebeau

Share this post


Link to post
Quote

Because Windows doesn't have good support for 32bit alpha-channeled bitmaps to begin with.

Lazarus(free pascal) can handle these bitmaps without any trouble.

 

Quote

Why don't you want to use a third party component? 

I try to avoid installing third party component because it complicates transferring my projects , it has to be installed, even worse when it's for a simple thing like displaying bitmap. Do you have any suggestion of component which could work in my case?

 

Quote

What complications does PNG present to you?  Are you working with VCL or FMX?  The VCL's TPNGImage class is easy to use.  FMX's TBitmap can be a bit difficult, depending on what you are trying to do with it.  It would really help if you would explain the problem you are having exactly.

Probably should not even mention that I use Delphi 7:classic_rolleyes: 

TBitmap allows access to individual pixels which I need to rearrange, no idea if TPNGImage can do that.

 

Edited by peter2005

Share this post


Link to post
25 minutes ago, Remy Lebeau said:

Because Windows doesn't have good support for 32bit alpha-channeled bitmaps

Really? I mean true for bmp files but Windows API supports 32 bit bitmaps fine so far as I know. 

  • Like 2

Share this post


Link to post
1 hour ago, peter2005 said:

I'm trying to display a 32bit picture data(with alpha channel) in a TImage using TBitmap and have not found a proper way

Would you show the code you currently use? It is always easier to answer a question when a minimal reproducible example is given. Provide the code and the required test images.

Share this post


Link to post

Works fine for me.

Here's a form with two TImage controls, one overlapping the other. The top one contains a 32-bit bitmap with alpha, the bottom a PNG.

image.png.3d65941735ec1435cb7f9965cf39504e.pngimage.png.e0d778e25f766a77524dc2d7ee1023be.png

 

I have set Image1.Transpartent=False and Image1.Picture.Bitmap.AlphaFormat=afDefined (at run-time) for the bitmap.

Edited by Anders Melander
  • Like 1

Share this post


Link to post
18 minutes ago, peter2005 said:

Probably should not even mention that I use Delphi 7

If you're using Delphi 7 then you will have to use 3rd party components. For example Graphics32 - and better hurry because I plan to drop support for Delphi 7 in Graphics32 the first chance I get.

  • Like 2

Share this post


Link to post

Here is small bmp image. There is a transparent background and greyishblack shadow at the bottom.

The background is not the problem Image1.Transpartent=False removes it, but the shadow is always fully black.

 

Can it be loaded and displayed properly?

1.bmp

Share this post


Link to post
39 minutes ago, peter2005 said:

Here is small bmp image. There is a transparent background and greyishblack shadow at the bottom.

The background is not the problem Image1.Transpartent=False removes it, but the shadow is always fully black.

Can it be loaded and displayed properly?

To use alpha transparency you need to set Transparent=False.

Here's what it looks like (Delphi 10.3):

image.png.43f9d3c19e1548f3e3c61870192d2447.png

  • Like 2

Share this post


Link to post
21 hours ago, Anders Melander said:

To use alpha transparency you need to set Transparent=False.

Here's what it looks like (Delphi 10.3):

image.png.43f9d3c19e1548f3e3c61870192d2447.png

Yes, it works, thanks.

 

Unfortunately Tbitmap in D7 does not have AlphaFormat property.

 

 

 

Edited by peter2005

Share this post


Link to post
On 1/30/2021 at 12:09 PM, Anders Melander said:

Image1.Picture.Bitmap.AlphaFormat=afDefined (at run-time) for the bitmap.

Oh yeah, I keep forgetting that TBitmap does have some alpha-channel support built-in.

Share this post


Link to post
On 2/2/2021 at 4:02 AM, Anders Melander said:

Yes but it's a nasty mess. Better than nothing I guess.

I wholeheartedly agree with you Anders that TBitmap.AlphaFormat is broken and should be deprecated.

Not only does it not behave in an intuitive manner, but Premultiplying and UnPremultiplying images is (mildly) destructive.

But there's another potentially bigger problem - when writing an afDefined Bitmap to stream, UnPreMultiplyAlpha is called and hence the image will no longer be properly rendered.

 

ps: Isn't it fun digging up old threads 😜.

 

Edited by angusj
typo

Share this post


Link to post
10 hours ago, angusj said:

ps: Isn't it fun digging up old threads 😜.

Yeah. It's good to get a reminder of this problem once in a while since it probably won't get fixed.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×