Doesn't look too bad, though your coding style scares the crap out of me :)
If you don't mind, I've modified the main unit a bit, I've fortunately been able to refrain myself from renaming controls and such :p so you can just replace the unit… There was a lot of stuff in there that didn't serve any purpose and I also renamed some variables to names that make somewhat more sense :)
[Removed Code]
I'd also recommend changing the label with "Compression factor" to "JPEG Quality" as compression would suggest that a higher number results in a higher compression / less quality.
EDIT:
So I was looking trough this again and I noticed you weren't freeing your resources, that's wrong in so many ways :) Also changed the opening of the directory a bit so it won't erase the box if you don't select one and it'll go to the directory in the textbox ;)
unit pngTroep;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, JPeg, StdCtrls, Pngimage, FileCtrl, ComCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Memo1: TMemo;
Edit2: TEdit;
Label1: TLabel;
UpDown1: TUpDown;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure UpDown1Click(Sender: TObject; Button: TUDBtnType);
procedure Edit2Change(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
begin
if DirectoryExists(Form1.Edit1.Text) then s := Form1.Edit1.Text;
if SelectDirectory('Choose the folder containing the PNG files.', '', s) then
Form1.Edit1.Text := s;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
SearchRec: TSearchRec;
Png: TPNGObject;
i: Integer;
Jpeg: TJPEGImage;
s: string;
tBmp: TBitmap;
begin
Png := TPNGObject.Create;
Jpeg := TJPEGImage.Create;
tBmp := TBitmap.Create;
try
i := StrToInt(Edit2.Text);
except
Memo1.Lines.Add('Now enter a number, PL0X!!!');
Exit;
end;
Jpeg.CompressionQuality := i;
SetCurrentDir(Edit1.Text);
if FindFirst('*.png', faAnyFile, SearchRec) = 0 then
begin
s := Trim(GetCurrentDir + '\' + SearchRec.Name);
memo1.lines.Add(s);
try
Application.ProcessMessages;
Png.LoadFromFile(s);
tBmp.Assign(Png);
Jpeg.Assign(tBmp);
s[Length(s) - 2] := 'j';
s[Length(s) - 1] := 'p';
Jpeg.SaveToFile(s);
except
memo1.lines.Add('An error occurred trying to convert: ' + s);
end;
end;
repeat
if FindNext(SearchRec) <> 0 then Break;
Jpeg.CompressionQuality := i;
s := Trim(GetCurrentDir + '\' + SearchRec.Name);
memo1.lines.Add(s);
try
Application.ProcessMessages;
Png.LoadFromFile(s);
tBmp.Assign(Png);
Jpeg.Assign(tBmp);
s[Length(s) - 2] := 'j';
s[Length(s) - 1] := 'p';
Jpeg.SaveToFile(s);
except
memo1.lines.Add('ERROR WITH FILE: ' + s);
end;
until False;
Memo1.Lines.Add('Done!');
FreeAndNil(Png);
FreeAndNil(Jpeg);
FreeAndNil(tBmp);
end;
procedure TForm1.UpDown1Click(Sender: TObject; Button: TUDBtnType);
begin
Edit2.Text := IntToStr(UpDown1.Position);
end;
procedure TForm1.Edit2Change(Sender: TObject);
begin
if Length(edit2.Text) > 0 then
if (StrToIntDef(edit2.Text, -1) < 0) then
begin
MessageBox(0, 'Please enter a number here!', 'No Number', MB_ICONERROR);
Edit2.Text := '40';
UpDown1.Position := 40;
end else
UpDown1.Position := StrToInt(edit2.Text);
end;
end.