Skip to content

Commit

Permalink
Updated PNG_GR32 example for FPC.
Browse files Browse the repository at this point in the history
Refs #69
  • Loading branch information
Anders Melander committed Jul 6, 2022
1 parent f80d88d commit 865de00
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 259 deletions.
55 changes: 42 additions & 13 deletions Examples/General/PNG Image32/MainUnit.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,59 @@ object FmPngDemo: TFmPngDemo
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
Padding.Left = 8
Padding.Top = 8
Padding.Right = 8
Padding.Bottom = 8
OldCreateOrder = False
OnCreate = FormCreate
DesignSize = (
475
279)
PixelsPerInch = 96
TextHeight = 13
object ImageDisplay: TImage32
Left = 8
Top = 8
Width = 459
Height = 262
Left = 0
Top = 38
Width = 475
Height = 241
Align = alClient
Bitmap.DrawMode = dmBlend
Bitmap.ResamplerClassName = 'TNearestResampler'
BitmapAlign = baTopLeft
Scale = 1.000000000000000000
ScaleMode = smNormal
TabOrder = 0
OnClick = ImageDisplayClick
OnDblClick = ImageDisplayDblClick
ExplicitTop = 8
ExplicitHeight = 262
end
object Panel1: TPanel
Left = 0
Top = 0
Width = 475
Height = 38
Align = alTop
BevelOuter = bvNone
TabOrder = 1
object CheckBoxFit: TCheckBox
Left = 184
Top = 11
Width = 97
Height = 19
Caption = 'Scale to fit'
TabOrder = 0
OnClick = CheckBoxFitClick
end
end
object ButtonLoad: TButton
Left = 8
Top = 8
Width = 75
Height = 25
Caption = 'Load...'
TabOrder = 2
OnClick = ButtonLoadClick
end
object ButtonSave: TButton
Left = 89
Top = 8
Width = 75
Height = 25
Caption = 'Save...'
TabOrder = 3
OnClick = ButtonSaveClick
end
end
35 changes: 0 additions & 35 deletions Examples/General/PNG Image32/MainUnit.lfm

This file was deleted.

126 changes: 80 additions & 46 deletions Examples/General/PNG Image32/MainUnit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
interface

uses
{$IFDEF FPC} LCLIntf, {$ELSE}Windows, Messages, {$ENDIF} SysUtils, Classes,
Graphics, Controls, Forms, Dialogs, GR32_Image;
{$IFDEF FPC} LCLIntf, {$ELSE} Windows, Messages, {$ENDIF}
SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
GR32_Image;

type
TFmPngDemo = class(TForm)
ImageDisplay: TImage32;
Panel1: TPanel;
ButtonLoad: TButton;
ButtonSave: TButton;
CheckBoxFit: TCheckBox;
procedure FormCreate(Sender: TObject);
procedure ImageDisplayClick(Sender: TObject);
procedure ImageDisplayDblClick(Sender: TObject);
procedure ButtonLoadClick(Sender: TObject);
procedure ButtonSaveClick(Sender: TObject);
procedure CheckBoxFitClick(Sender: TObject);
end;

var
Expand All @@ -22,61 +28,89 @@ implementation
uses
GR32_PNG, GR32_PortableNetworkGraphic;

{$IFDEF FPC}
{$R *.lfm}
{$ELSE}
{$R *.dfm}
{$ENDIF}

procedure TFmPngDemo.FormCreate(Sender: TObject);
procedure TFmPngDemo.ButtonLoadClick(Sender: TObject);
var
OpenDialog: TOpenDialog;
PNG: TPortableNetworkGraphic32;
begin
if FileExists('..\Demo.png') then
with TPortableNetworkGraphic32.Create do
try
LoadFromFile('..\Demo.png');
AssignTo(ImageDisplay.Bitmap);
finally
Free;
end
else
raise Exception.Create('File not found: Demo.png');

ClientWidth := ImageDisplay.Bitmap.Width + 16;
ClientHeight := ImageDisplay.Bitmap.Height + 16;
end;
OpenDialog := TOpenDialog.Create(Self);
try
OpenDialog.Filter := 'PNG Images (*.png)|*.png|All files (*.*)|*.*';
OpenDialog.DefaultExt := '.png';
if not OpenDialog.Execute then
exit;

procedure TFmPngDemo.ImageDisplayClick(Sender: TObject);
begin
with TSaveDialog.Create(Self) do
PNG := TPortableNetworkGraphic32.Create;
try
Filter := 'PNG Images (*.png)|*.png';
DefaultExt := '.png';
if Execute then
begin
with TPortableNetworkGraphic32.Create do
try
AdaptiveFilterMethods := [aafmSub, aafmUp, aafmAverage];
Assign(ImageDisplay.Bitmap);
InterlaceMethod := imAdam7;
SaveToFile(FileName);
finally
Free;
end;
end;
PNG.LoadFromFile(OpenDialog.FileName);
PNG.AssignTo(ImageDisplay.Bitmap);
finally
Free;
PNG.Free;
end;
finally
OpenDialog.Free;
end;
end;

procedure TFmPngDemo.ImageDisplayDblClick(Sender: TObject);
procedure TFmPngDemo.ButtonSaveClick(Sender: TObject);
var
SaveDialog: TSaveDialog;
PNG: TPortableNetworkGraphic32;
begin
with TPortableNetworkGraphic32.Create do
SaveDialog := TSaveDialog.Create(Self);
try
SaveDialog.Filter := 'PNG Images (*.png)|*.png|All files (*.*)|*.*';
SaveDialog.DefaultExt := '.png';
if not SaveDialog.Execute then
exit;

PNG := TPortableNetworkGraphic32.Create;
try
Assign(ImageDisplay.Bitmap);
SaveToFile('Test.png');
PNG.AdaptiveFilterMethods := [aafmSub, aafmUp, aafmAverage];
PNG.Assign(ImageDisplay.Bitmap);
PNG.InterlaceMethod := imAdam7;
PNG.SaveToFile(SaveDialog.FileName);
finally
Free;
PNG.Free;
end;
finally
SaveDialog.Free;
end;
end;

procedure TFmPngDemo.CheckBoxFitClick(Sender: TObject);
begin
if (TCheckBox(Sender).Checked) then
begin
ImageDisplay.ScaleMode := smResize;
ImageDisplay.BitmapAlign := baCenter;
end else
begin
ImageDisplay.ScaleMode := smNormal;
ImageDisplay.BitmapAlign := baTopLeft;
end;
end;

procedure TFmPngDemo.FormCreate(Sender: TObject);
begin
{$IFNDEF FPC}
ImageDisplay.Margins.Top := 8;
ImageDisplay.Margins.Left := 8;
ImageDisplay.Margins.Bottom := 8;
ImageDisplay.Margins.Right := 8;
ImageDisplay.AlignWithMargins := True;
{$ENDIF}

if FileExists('..\Demo.png') then
with TPortableNetworkGraphic32.Create do
try
LoadFromFile('..\Demo.png');
AssignTo(ImageDisplay.Bitmap);
finally
Free;
end;
end;

end.
4 changes: 1 addition & 3 deletions Examples/General/PNG Image32/PNG_GR32_Demo.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ program PNG_GR32_Demo;

uses
Forms,
MainUnit in 'MainUnit.pas' {FmPngDemo},
GR32_Png in '..\GR32_Png.pas',
GR32_PortableNetworkGraphic in '..\GR32_PortableNetworkGraphic.pas';
MainUnit in 'MainUnit.pas' {FmPngDemo};

{$R *.res}

Expand Down
2 changes: 0 additions & 2 deletions Examples/General/PNG Image32/PNG_GR32_Demo.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@
<DCCReference Include="MainUnit.pas">
<Form>FmPngDemo</Form>
</DCCReference>
<DCCReference Include="..\GR32_Png.pas"/>
<DCCReference Include="..\GR32_PortableNetworkGraphic.pas"/>
<BuildConfiguration Include="Base">
<Key>Base</Key>
</BuildConfiguration>
Expand Down

0 comments on commit 865de00

Please sign in to comment.