unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Windows,
BGRABitmap, BGRABitmapTypes,
fpCanvas, IntfGraphics, LazCanvas,
Classes , SysUtils , Forms , Controls , Graphics , Dialogs , ExtCtrls ,
StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
Image2: TImage;
Image3: TImage;
procedure Button1Click(Sender: TObject);
procedure FormResize(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
procedure CreateRescaleBitmap(const Source: TBitmap; const Width, Height: Integer; out Rescaled: TBitmap);
var
DC: HDC;
MemDC: HDC;
Bitmap: HBITMAP;
OldBitmap: HBITMAP;
begin
DC := GetDC(0);
try
MemDC := CreateCompatibleDC(DC);
try
Bitmap := CreateCompatibleBitmap(DC, Width, Height);
OldBitmap := SelectObject(MemDC, Bitmap);
SetStretchBltMode(MemDC, HALFTONE);
SetBrushOrgEx(MemDC, 0, 0, nil);
StretchBlt(MemDC, 0, 0, Width, Height, Source.Canvas.Handle, 0, 0, Source.Width, Source.Height, SRCCOPY);
Rescaled := TBitmap.Create;
Rescaled.Assign(Source);
Rescaled.Width := Width;
Rescaled.Height := Height;
BitBlt(Rescaled.Canvas.Handle, 0, 0, Width, Height, MemDC, 0, 0, SRCCOPY);
SelectObject(MemDC, OldBitmap);
DeleteObject(Bitmap);
finally
DeleteDC(MemDC);
end;
finally
ReleaseDC(0, DC);
end;
end;
procedure AntiAliasedStretchDrawBitmap(SourceBitmap, DestBitmap: TCustomBitmap);
var
DestIntfImage, SourceIntfImage: TLazIntfImage;
DestWidth, DestHeight: Integer;
DestCanvas: TLazCanvas;
begin
DestWidth := DestBitmap.Width;
DestHeight := DestBitmap.Height;
DestIntfImage := TLazIntfImage.Create(0, 0);
try
DestIntfImage.LoadFromBitmap(DestBitmap.Handle, DestBitmap.MaskHandle);
DestCanvas := TLazCanvas.Create(DestIntfImage);
try
SourceIntfImage := SourceBitmap.CreateIntfImage;
try
DestCanvas.Interpolation := TFPBaseInterpolation.Create;
try
DestCanvas.StretchDraw(0, 0, DestWidth, DestHeight, SourceIntfImage);
DestBitmap.LoadFromIntfImage(DestIntfImage);
finally
DestCanvas.Interpolation.Free;
end;
finally
SourceIntfImage.Free;
end;
finally
DestCanvas.Free;
end;
finally
DestIntfImage.Free;
end;
end;
function Resample(const ASource: TBGRABitmap; const AWidth, AHeight: Integer): TBGRABitmap;
begin
ASource.ResampleFilter := rfBestQuality;
Result := ASource.Resample(AWidth, AHeight);
end;
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
bgr1, bgr2: TBGRABitmap;
png1, png2: TPortableNetworkGraphic;
bmp1, bmp2: TBitmap;
begin
if FileExists('.\HowItShouldLook.png') then
begin
// Image1 @ GDI+
png1 := TPortableNetworkGraphic.Create;
bmp1 := TBitmap.Create;
try
png1.LoadFromFile('.\HowItShouldLook.png');
bmp1.Assign(png1);
CreateRescaleBitmap(bmp1, Image1.Width, Image1.Height, bmp2);
Image1.Proportional := True;
Image1.Stretch := True;
Image1.Picture.Assign(bmp2);
finally
bmp2.Free;
bmp1.Free;
png1.Free;
end;
// Image2 @ BGRA
bgr1 := TBGRABitmap.Create;
bgr2 := TBGRABitmap.Create;
try
bgr1.LoadFromFile('.\HowItShouldLook.png');
bgr2 := Resample(bgr1, Image2.Width, Image2.Height);
Image2.Proportional := True;
Image2.Stretch := True;
Image2.Picture.Assign(bgr2);
finally
bgr2.Free;
bgr1.Free;
end;
// Image3 @ Canvas
png1 := TPortableNetworkGraphic.Create;
bmp1 := TBitmap.Create;
bmp2 := TBitmap.Create;
try
png1.LoadFromFile('.\HowItShouldLook.png');
bmp1.Assign(png1);
bmp2.SetSize(Image3.Width, Image3.Height);
AntiAliasedStretchDrawBitmap(bmp1, bmp2);
Image3.Proportional := True;
Image3.Stretch := True;
Image3.Picture.Assign(bmp2);
finally
bmp2.Free;
bmp1.Free;
png1.Free;
end;
(*
// classical not pretty windows method
png1 := TPortableNetworkGraphic.Create;
png2 := TPortableNetworkGraphic.Create;
try
png1.LoadFromFile('.\HowItShouldLook.png');
png2.SetSize(Image1.Width, Image1.Height);
StretchBlt(png2.Canvas.Handle, //destination HDC
0, 0, Image1.Width, Image1.Height, // destination size
png1.Canvas.Handle, //source HDC
0, 0, png1.Width, png1.Height, // source size
SrcCopy);
Image1.Picture.Assign(png2);
finally
png2.Free;
png1.Free;
end;
*)
end;
end;
procedure TForm1.FormResize(Sender: TObject);
begin
Image1.Width := Form1.ClientWidth div 3;
Image2.Width := Form1.ClientWidth div 3;
end;
end.