3

I have a TImage into which I loaded a PNG with transparency. David hinted me how to give it a bitmap to draw on:

var
  Png: TPngImage;
  Bmp: TBitmap;
begin
  Png := TPngImage.Create;
  Bmp := TBitmap.Create;
  try
    Png.LoadFromResourceName(HInstance, 'background');
    Bmp.Assign(Png);
    Image1.Picture.Assign(Bmp);
  finally
    Png.Free;
    Bmp.Free;
  end;

  with Image1, Canvas do
  begin
    Pen.Width := 7;
    Pen.Color := clBlue;
    MoveTo(0, 0);
    LineTo(150, 100);
  end;
end;

I can draw on the image's canvas, but the color I defined for my Pen is ignored; instead all lines appear gray. I realized that I must be drawing in the alpha channel instead of the RGB channels, which I could confirm by putting another image underneath. (The gray I got is the color of the underlying TForm.)

enter image description here

The clock-face is opaque, and the area around it transparent which allows you to see the cityscape on the image underneath. So instead of having a blue hand on the clock the hand becomes transparent. (I extended the hand to go over the area which was already transparent, but it doesn't seem to change anything there.)

Why am I drawing in the alpha channel, and how can I make Delphi draw in the RGB channels instead?

update
I uploaded a minimal project which should allow you to reproduce the problem here.

Joris Groosman
  • 771
  • 8
  • 23
  • What do you mean by drawing "in" the alpha channel. Do you mean that the pre-existing alpha channel is remaining. – David Heffernan Mar 01 '16 at 08:08
  • What I mean is that wherever I draw the image becomes fully transparent, so I can see what's underneath. – Joris Groosman Mar 01 '16 at 09:42
  • It might be nice if we could repro this. As it stands I don't feel inclined to dig into this because I've no confidence that I would be using the same code as you. – David Heffernan Mar 01 '16 at 09:44
  • Try `Pen.Style := psSolid;` also try to remove the `with` statement (as a test) to be sure that you set things where you wanted to. – Hans Mar 01 '16 at 10:16
  • @DavidHeffernan - At the moment the code is part of a larger program. I'll see if I can reduce it to its essentials and upload that project – Joris Groosman Mar 01 '16 at 10:30
  • @Hans - Didn't work. Thanks for your reply though. – Joris Groosman Mar 01 '16 at 10:30
  • Is `Image1` perhaps in transparent mode? In `tmAuto` it will use the colour of the lower left pixel as the transparent colour. If the clock image originally had a blue background this could explain the behaviour. – J... Mar 01 '16 at 10:45
  • @DavidHeffernan - I added a link where you can download the program at the bottom of my question. – Joris Groosman Mar 01 '16 at 11:22
  • 1
    You can't paint a bitmap with an alpha channel with plain gdi. You can get some results by changing pen mode, but the behavior is not defined. As an example set pen mode to R2_NOTCOPYPEN and color to 00FFFF (inverse of clBlue). Anyway, use AlphaBlend or set each pixel individually calculating the alpha yourself. – Sertac Akyuz Mar 01 '16 at 12:32
  • Joris, links to external sites are no good. For one future readers will probably meet a non-functional link, and I for one, will never create an account and give out my email in such a file sharing service just for the purpose of downloading your problem. Include the code in your question and make sure it is as compact as possible to demonstrate the problem. – Tom Brunberg Mar 02 '16 at 06:50
  • @TomBrunberg - I added the fink in the first place for David. Did you read his comment? After I edited the question it has now almost all the code for the form. I feel the resources I use, in particular the PNG file, might be relevant too, and SO won't allow me to upload binary files. (I'm not certain that images you upload aren't converted by the system.) – Joris Groosman Mar 02 '16 at 11:08
  • @Joris - Specific png is not important, it is easy to duplicate your test case. My comment is accurate, gdi has very limited support for partially transparent images. – Sertac Akyuz Mar 02 '16 at 13:47

0 Answers0