1

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.

enter image description here enter image description here

Arnold
  • 4,578
  • 6
  • 52
  • 91
  • Could the downvoter please explain why? If some aspects of the question are unclear I'll be happy to explain. – Arnold Sep 24 '17 at 19:18
  • Nobody will find the question without the generic delphi tag. The version specific delphi-xe5 tags tells us which particular version you use. Don't remove the delphi tag again. – David Heffernan Sep 24 '17 at 21:54
  • Thanks! So the tags are delphi delphi-xe5 and bitmap. If I do not add any subject (in this case bitmap) nobody will look. – Arnold Sep 25 '17 at 02:04
  • Bitmap is pointless, but it doesn't matter – David Heffernan Sep 25 '17 at 05:33
  • Regarding tags, `firemonkey` is useful, because the `fmx` library is so different from the `vcl`. Anyway, I tried to recreate the problem in XE7 but did not succeed. What is your target platform? I tested with Windows 7. – Tom Brunberg Sep 25 '17 at 06:45
  • 1
    Out of curiosity I tested also in XE5 but still can't reproduce the problem. You must do something strange in code you haven't shown. – Tom Brunberg Sep 25 '17 at 09:54
  • @Tom Brunberg, Thanks for trying. yes, the target is Windows 7. I added the complete code sequence leading to the call of this procedure. I didn't show it before because there was no image manipulation there and I don't see how it could cause this weird behavior. Nevertheless, there must be some blunder somewhere as this code is so simple :-(. – Arnold Sep 26 '17 at 07:30
  • 1
    With some corrections to make it compilable - dir variables missing - and some hand made images with text in them I was not able to reproduce this. But when I used some of the things in my pictures folder I saw your issue. The images used when the issue occurred were very different in size - screenshots 1924x1164 and a temporary icon 16x8 pixels. – nil Sep 26 '17 at 12:28
  • 1
    @TomBrunberg no not really. From what I seem to experience the issue appears when an image is actually smaller as the thumbnail should be. – nil Sep 26 '17 at 12:33
  • 1
    @Nil, I will see if that creates the problem here too, were the images bmp's jpg's or something else? – Tom Brunberg Sep 26 '17 at 12:35
  • 1
    @TomBrunberg two small bmps (16x8 px) one png screenshot. – nil Sep 26 '17 at 12:36
  • 1
    @Nil , nil success here! With 16x8 bmp's the result was 64x64 black bg with the 16x8 as fg with unchaged size. Also tested with mid-sized bmp's and large .png. No two images mixed into one. Pitty. – Tom Brunberg Sep 26 '17 at 13:07
  • 1
    Sorry Arnold, I was not able to reproduce the error even with the additional code (but tested only with .bmp images). I had to invent some missing declarations, create some directories and testimages, but I'm confident they can not affect the outcome. I will have to drop the issue, but if you can (and have interest to) create a standalone Minimal, Complete and Verifiable Example , with emphasis on minimal, I would be interested to investigate further. – Tom Brunberg Sep 26 '17 at 13:08
  • Hi guys, all +1 for trying, I appreciate your efforts very much, Thank you all! The two images I showed as examples were both foto's (jpg's) around the 2k solution, so definitely much larger than the thumbnail size. But I got the same results when testing with smaller (>64 width and height) images. @Tom, You gave me a workaround by providing an answer in another question of mine that solved this issue (https://stackoverflow.com/questions/46398851/how-to-stretch-image-in-firemonkey) . When I have time I'll try to find a minimal version, but the need for that is somewhat dominished. – Arnold Sep 27 '17 at 16:48
  • 1
    Yes I understood the other solved the case, it's just my curiosity what the reason could be for such highly unexpected behaviour. But as you say whenever there's some spare time. Cheers – Tom Brunberg Sep 27 '17 at 17:00
  • @Tom, this behavior is highly unexpected indeed. When you look at the code one cannot understand what is the cause of this. It must be some sneeky mistake I have made somewhere else, but I couldn't find out what. Anyuways, thanks for your help! – Arnold Sep 28 '17 at 17:50

0 Answers0