3
我有一個大的(300x300px)PNG圖像,我需要重新縮放。在其他SO question我讀了關於StretchBlt
和HALFTONE
爲了更好的縮放。繪製透明縮放PNG
我的問題是我如何繪製PNG圖像透明,或至少塗黑的角落白色?
正如你所附加的圖像上看到我得到黑色的角落。
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.
你想整個形象是透明的,或「不畫的角落」?後者會是一個問題,因爲它們是黑色的,而且我認爲,「繪製*白色黑色的所有東西」都可以做到這一點,但不是您尋求的解決方案。你需要一個覆蓋角落的面具。真的,擺脫角落最簡單的方法是在圖像編輯器中打開圖像。 – usr2564301
首選我希望角落是透明的,如果那很難做,那麼角子機將變成白色。 –
該圖像是您在此刻獲得的屏幕截圖。你還可以顯示原始圖像嗎?如果您無法編輯原始圖像,是否可以使用另一張圖像作爲蒙版? – usr2564301