Michael
Michael

Reputation: 125

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:

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

Thanks!

Upvotes: 6

Views: 2403

Answers (1)

David Heffernan
David Heffernan

Reputation: 613013

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?

Upvotes: 6

Related Questions