Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparent image control with resampling in Delphi

I have a form with a background image (painted on the form in Form1.Repaint).

What I am a looking for: A transparent image control, that can smoothly resize (resample) the loaded image.

(I need it to be transparent because the forms background image should be visible through)

What I've tried:

  • Standard TImage: It's transparent, but it does not resample.

  • Graphics32 / Image32: Resamples beautifully, but it's not transparent.

    I have googled for several hours now for fixes or work-arounds, but without much of a solution. This has nothing to do with the image loaded into Image32 being transparent, but instead the background color of the control still being white (white = the color-property of the Image32 control, and setting it to clNone does not work). This is apparently as designed

  • GR32ex (The GR32 Extension Components Pack), which supposedly adds a Transparent-property, however it has not been updated in many years, and I can not install it. It throws a gazillion errors on Delphi 2010 and Graphics32 v. 1.9.

Can anybody think of a solution or workaround? All I want is a control with transparency and resampling.

Thanks!

like image 514
Michael Avatar asked Jun 25 '12 13:06

Michael


1 Answers

I'm surprised that TImage32 doesn't do transparency. Are you really sure that is the case?

Anyway, if that is so, I would combine the transparency support of TImage with the re-sampling ability of TBitmap32 to build a solution that way. Keep the original image in a TBitmap32 instance. Whenever you need to load it into the TImage component, for example when re-sizing, use TBitmap32 to perform an in-memory re-size and load that re-sized image.

In fact, if you are already painting the form's background yourself, why not paint the image yourself and simply do away with the image control?

Update 1: Websearch reveals a simple way to make TImage32 transparent: http://graphics32.org/news/newsgroups.php?art_group=graphics32.general&article_id=9505

Update 2: The link above is now dead, and the newsgroups can only be accessed via NNTP. I can't be 100% certain, but I think that the linked post was by Michael Haralabos and contained the following file:

unit GR32_ImageEx;

// Transparent TImage32 by Michael Haralabos

interface

uses
  Windows, Messages, Classes, GR32_Image, GR32;

type
  TImage32Ex = class(TImage32)
  private
    FTransparent: Boolean;

    procedure SetTransparent(const Value: Boolean);
  public
    procedure ExecClearBackgnd(Dest: TBitmap32; StageNum: Integer); override;
  published
    property Enabled;
    property Transparent: Boolean read FTransparent write SetTransparent;
  end;

procedure Register;

implementation

procedure TImage32Ex.ExecClearBackgnd(Dest: TBitmap32; StageNum: Integer);
var
  P: TPoint;
  SaveIndex: Integer;
begin
  if FTransparent and Assigned(Parent) and
     not (Assigned(Bitmap) and (BitmapAlign = baTile)) then
  begin
    SaveIndex := SaveDC(Dest.Handle);
    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);
    RestoreDC(Dest.Handle, SaveIndex);
  end
  else
    inherited;
end;

procedure TImage32Ex.SetTransparent(const Value: Boolean);
begin
  if FTransparent <> Value then
  begin
    FTransparent := Value;
    Invalidate;
  end;
end;

procedure Register;
begin
  RegisterComponents('Graphics32', [TImage32Ex]);
end;

end.

Another topic here suggests that this may be what the now dead link referred to: Delphi TImage32 - how to make the component invisible if no picture is loaded?

like image 56
David Heffernan Avatar answered Sep 26 '22 00:09

David Heffernan