Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TImage32 trasparent background like TImage #105

Open
D4n16l6 opened this issue Sep 28, 2019 · 3 comments
Open

TImage32 trasparent background like TImage #105

D4n16l6 opened this issue Sep 28, 2019 · 3 comments

Comments

@D4n16l6
Copy link

D4n16l6 commented Sep 28, 2019

Hello, I am working with Delphi 2007 in Windows 10 and I would like to use the TImage32 instead of the TImage but while the TImage allows to have the transparent background the TImage32 always shows a background color. Is there a way to replicate the same behavior as the Image?

@carlosbfeitozafilho
Copy link

carlosbfeitozafilho commented Mar 21, 2020

This behaviour is very annoying indeed! I'm trying to use TImage32 on my Delphi Rio "VCL Styled" project and the component shows the background. My intention is to have a simple image viewer for my ID3v2 powered MP3 (See the picture). In fact I've made it, using TImage, but it do not have resample routines attached, so, the images are pixelated when shrinked but, at least, the area around the image is transparent
Clipboard01

@andersmelander
Copy link
Member

There is a trick, originally authored by Michael Haralabos, that can make TImage32 transparent. I have adapted it to be used as an interposer class:

unit GR32_ImageTransparent;

interface

uses
  GR32,
  GR32_Image;

type
  TImage32 = class(GR32_Image.TImage32)
  private
    FTransparent: Boolean;
  protected
    procedure SetTransparent(const Value: Boolean);
  protected
    procedure ExecClearBackgnd(Dest: TBitmap32; StageNum: Integer); override;
  published
    property Transparent: Boolean read FTransparent write SetTransparent;
  end;

implementation

uses
  Windows,
  Messages;

procedure TImage32.ExecClearBackgnd(Dest: TBitmap32; StageNum: Integer);
var
  P: TPoint;
  SaveIndex: Integer;
begin
  if (not FTransparent) or (Parent = nil) or
    ((Bitmap <> nil) and ((BitmapAlign = baTile) or (ScaleMode = smStretch))) then
  begin
    inherited;
    exit;
  end;

  SaveIndex := SaveDC(Dest.Handle);
  try
    GetViewportOrgEx(Dest.Handle, P);
    SetViewportOrgEx(Dest.Handle, P.X - Left, P.Y - Top, nil);

    IntersectClipRect(Dest.Handle, 0, 0, Parent.ClientWidth, Parent.ClientHeight);

    Parent.Perform(WM_ERASEBKGND, Dest.Handle, 0);
    Parent.Perform(WM_PAINT, Dest.Handle, 0);
  finally
    RestoreDC(Dest.Handle, SaveIndex);
  end;
end;

procedure TImage32.SetTransparent(const Value: Boolean);
begin
  if (FTransparent = Value) then
    exit;

  FTransparent := Value;
  Invalidate;
end;

end.

To use it add the unit to the uses clause after GR32_Image. Like so:

uses
  {$IFNDEF FPC} Windows, {$ELSE} LCLIntf, LResources, {$ENDIF}
  SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
  GR32, GR32_Image, GR32_Resamplers, GR32_RangeBars,
  GR32_ImageTransparent;

type
  TFormImage32Example = class(TForm)
    Image: TImage32;
    PnlControl: TPanel;
    RgpScaleMode: TRadioGroup;
    RgpKernel: TRadioGroup;
...

...and set the Image.Transparent property to True in code.
Here's what the standard TImage32 demo looks like with the change. I've placed a TImage and a TMemo behind the TImage32 and loaded a semitransparent bitmap into the TImage32. As you can see the transparency works with the TImage but not with the TMemo:
image

I would like to merge this enhancement into the standard TImage32 but I'm a bit hesitant to do so.
First of all it's very rudimentary and as you can see we can expect that there will be scenarios that it doesn't handle (with regard to what's behind the TImage32). Once it's in there people will expect it to handle everything.
Secondly it will need to be tweaked to support FPC.
Finally I think it needs to take backends (TBitmap32.Backend) into account and that's an area where I have no knowledge.

@CWBudde
Copy link
Contributor

CWBudde commented Sep 27, 2020

I once came up with a similiar solution like the one you described, but for the very same reasons (does not work in all situations, backend support, FPC), I did not add the code to the main repository.

This said, I think some support would be at least useful. Maybe not on the TImage32 level, but eventually on a new TTransparentImage32 descendant. Eventually this could get a Delphi standard-backend implementation only, to avoid FPC / backend problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants