I have a weird problem and am not able to solve it. I have a lot of images and I want to create thumbnails of them. I point my application to a directory and it creates thumbnails (64 * 64) of each of them. The trouble is that the previous bitmap persists in a new bitmap which I don't understand. Here is the code of the procedure causing the error:
procedure TMain.import_image_resize (source, destination: string);
var
Input_Bitmap: TBitmap;
begin
Input_Bitmap := TBitmap.CreateFromFile (source);
Input_Bitmap.ReSize (iSize, iSize); // iSize = 64
Input_Bitmap.SaveToFile (destination);
Input_Bitmap.Free;
end; // import_image_resize //
It is called as an argument by import_process_images
, below.
procedure TMain.import_process_images (sub: string; process: TConverter);
var
i, n: int32;
dir_input: string;
dir_new: string;
temp: string;
file_path: string;
file_name: string;
file_ext: string;
new_file: string;
source_dirs: TStringDynArray;
destination_dirs: TStringDynArray;
files: TStringDynArray;
begin
// get list of directories from selected directory
source_dirs := TDirectory.GetDirectories (Dir_Selected);
SetLength (destination_dirs, Length (source_dirs));
// create these directories in the destination directory
n := 0;
for dir_input in source_dirs do
begin
i := LastDelimiter ('\', dir_input) - 1;
temp := dir_input.Substring (i + 1);
dir_new := TPath.Combine (Project_Root, Project_Selected);
dir_new := TPath.Combine (dir_new, sub);
dir_new := TPath.Combine (dir_new, temp);
TDirectory.CreateDirectory (dir_new);
destination_dirs [n] := dir_new;
n := n + 1;
end; // for
// for each directory in the selected directory
// - get each image
// - convert it
// - and copy it to the destination directory
n := 0;
Stop_Conversion := False;
for dir_new in source_dirs do
begin
files := TDirectory.GetFiles (dir_new);
for file_path in files do
begin
file_name := TPath.GetFileName (file_path);
file_ext := LowerCase (TPath.GetExtension (file_name));
if (file_ext = '.bmp') or (file_ext = '.jpg') or
(file_ext = '.png') or (file_ext = '.jpeg') then
begin
new_file := TPath.Combine (destination_dirs [n], file_name);
process (file_path, new_file);
Label_Progress.Text := new_file;
Application.ProcessMessages;
if Stop_Conversion then Exit;
end; // if
end; // for
n := n + 1;
end; // for
Label_Progress.Text := 'Ready';
end; (*** import_process_images ***)
Both functions are called from the event handler as follows:
procedure TMain.Button_SelectClick (Sender: TObject);
var
tree_item: TTreeViewItem;
begin
iSize := StrToInt (edit_XSize.Text);
tree_item := Directory_Tree.Selected;
Dir_Selected := tree_item.Text;
import_process_images ('rs', import_image_resize);
end; // Button_SelectClick //
One would expect that the new Input_Bitmap should be only filled with the bitmap loaded from file. However, the resized bitmap shows all images of previous bitmaps (loaded by previous calls from import_image_resize) overlayed with the current one. I don't understand this behavior, anybody got an explanation and, preferrably, a workaround?
Thanks you for your time.
Edit 1 I'll show an example of two photo's successively converted: the first is a landscape photo, the second in portrait. You see the first photo at the edges of the second photo. The second photo just overlayed the first one (the third overlayes the combination of the first two, etc.)
Edit 2
There was a suggestion that some code not shown might have impact on the procedure import_image_resize
. As for completeness I added this code but I cannot see my self what exactly I am doing wrong.