title: Draw Transparent Scaled PNG
body: <p>I have a large  (300x300px) PNG image which I need to rescale. In an other <a href="https://stackoverflow.com/questions/1976116/scale-an-image-nicely-in-delphi">SO question</a> I read about <code>StretchBlt</code> and <code>HALFTONE</code> in order for better scaling.</p>
  
  <p>My problem is how do I draw the  PNG image transparent, or at least paint the black corners white? </p>
  
  <p><a href="https://i.stack.imgur.com/XtRmk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XtRmk.png" alt="enter image description here"></a></p>
  
  <p>As you can see on the attached image I get black corners. </p>
  
  <p>And here is the original image:
  <a href="https://i.stack.imgur.com/ayZAO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ayZAO.png" alt="enter image description here"></a>
  Here is what I've tried so far. </p>
  
  <pre><code>unit MainU;
  
  interface
  
  uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, ExtCtrls;
  
  type
    TFormMain = class(TForm)
      procedure FormCreate(Sender: TObject);
    private
      { Private declarations }
    public
      { Public declarations }
    end;
  
  var
    FormMain: TFormMain;
  
  implementation
  
  {$R *.dfm}
  uses
    Pngimage;
  
  type
    TIRButton = class(TImage)
    protected
      procedure Paint; override;
    public
      constructor Create(AOwner: TComponent); override;
    end;
  
    TGraphicControlAcess = class(TGraphicControl);
    TGraphicAcess = class(TGraphic);
  
    THalfTonePngImage = class(TPngImage)
    protected
      procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
    end;
  
    { TIRButton }
  
  constructor TIRButton.Create(AOwner: TComponent);
  begin
    inherited;
    Center := True;
    Proportional := True;
  end;
  
  procedure TIRButton.Paint;
  var
    ParentCanvas: TCanvas;
  begin
    ParentCanvas := TGraphicControlAcess(Self).Canvas;
    TGraphicAcess(Picture.Graphic).Draw(ParentCanvas, DestRect);
  end;
  
  { THalfTonePngImage }
  
  procedure THalfTonePngImage.Draw(ACanvas: TCanvas; const Rect: TRect);
  var
    p: TPoint;
    dc: HDC;
  begin
    dc := ACanvas.Handle;
    GetBrushOrgEx(dc, p);
    SetStretchBltMode(dc, HALFTONE);
    SetBrushOrgEx(dc, p.X, p.Y, @p);
  
    ACanvas.Brush.Color := clWhite;
    ACanvas.FillRect(Classes.Rect(0, 0, Width, Height));
  
    StretchBlt(
      dc, 0, Rect.Top, Rect.Right - Rect.Left, Rect.Bottom - Rect.Top,
      Canvas.Handle, 0, 0, Width, Height, ACanvas.CopyMode
      );
  end;
  
  procedure TFormMain.FormCreate(Sender: TObject);
  var
    Image: THalfTonePngImage;
  begin
    Image := THalfTonePngImage.Create;
    Image.LoadFromFile('X200IR_11_EmgBrake.png');
  
    with TIRButton.Create(Self) do
    begin
      Width := 100;
      Height := 100;
      Picture.Assign(Image);
      Parent := Self;
      Anchors := [akLeft, akTop, akRight, akBottom];
    end;
  
    Image.Free;
  end;
  
  end.
  </code></pre>