Is it possible to remove hideous outline around a TSpeedButton glyph?
Asked Answered
L

1

21

I've run into a bit of a snag, is it just me or can you not assign an image from a resource to TSpeedButton's glyph without a hideous black outline as shown below?

I've assigned it exactly the same way for the TImage component and I'm getting the result needed.

I've been searching for quite a while but no one seems to have this bizarre and annoying problem.

Here's my source code for the form below:

procedure TForm3.Button1Click(Sender: TObject);
var r : tresourcestream; png : tpngimage;
begin
  r := tresourcestream.CreateFromID(hinstance,34,'cardimage');
  png := tpngimage.Create;  
  png.LoadFromStream(r);  
  png.AssignTo(image1.Picture.bitmap);  
  png.AssignTo(speedbutton1.glyph);  
  png.Free;  
  r.Free;  
end;

34 is the image of type 'cardimage' that relates to the image being shown in the picture if you haven't guessed already.

enter image description here

Leandroleaning answered 14/3, 2011 at 22:43 Comment(2)
The issue is clearly that the alpha channel is ignored in the left picture.Halflight
Generally, you should call X.Assign(Y), not Y.AssignTo(X). If TX doesn't know how acquire attributes from a TY, it will defer to TY by calling Y.AssignTo(X) automatically. But if TY doesn't know how to assign itself to a TX, it won't defer to the target object.Cnidoblast
H
33

The issue is clearly that the alpha channel is ignored in the left picture. Now, the TSpeedButton.Glyph property is a TBitmap, so it might be problematic to preserve the PNG alpha channel. For example,

var
  png: TPNGImage;
begin
  png := TPngImage.Create;
  png.LoadFromFile('C:\Users\Andreas Rejbrand\Pictures\alpha.png');
  SpeedButton1.Glyph.Assign(png); // or png.AssignTo(SpeedButton1.Glyph);

produces

One partial solution is to pre-blend the PNG image:

var
  png: TPNGImage;
  bm: TBitmap;
begin
  png := TPngImage.Create;
  png.LoadFromFile('C:\Users\Andreas Rejbrand\Pictures\alpha.png');
  bm := TBitmap.Create;
  bm.SetSize(png.Width, png.Height);
  bm.Canvas.Brush.Color := Self.Color;
  bm.Canvas.FillRect(Rect(0, 0, bm.Width, bm.Height));
  bm.Canvas.Draw(0, 0, png);
  SpeedButton1.Glyph.Assign(bm);

Halflight answered 14/3, 2011 at 23:0 Comment(7)
Thanks, @Ken. But there might be a better solution.Halflight
I would +1 if I could lol, but Thanks works a treat! It's only being used for a school project, and was just annoying my eyes lol but thanks yet again! :DLeandroleaning
pre-blending's going to gîte you some grief somewhere down the line.Binomial
Nit-pick :) guess you don't need a second bitmap, can draw directly to the button glyph..Topology
@Sertac: Oh, yes, quite true.Halflight
FWIW, I am personally going to rewrite TSpeedButton to have proper PNG support. Some day. When I have time.... [grins]Baeza
Very nice, this is something that should be native. Amazing even in XE2 the buttons still dont support PNG by default.Swordplay

© 2022 - 2024 — McMap. All rights reserved.