delphiresizetimagetscrollbox

How do I reset the scrolling after removing images in TScrollBox?


I have a Delphi form with a TScrollBox and some TImage components, and the form's scrollbox is not resetting when it's emptied... it appears to be growing every time a new image is thrown in the box.

I would like to reset the scroll range/size to the scrollbox size after I delete the image, before the next one is loaded. Is there a way to do that?

I've tried setting scrollbars invisible, and turning them back on after the next file loads and that doesn't seem to work. Any help greatly appreciated.

Root Cause: So it appears that the image moves its top-left corner to the center of where the image was located in the TScrollBox when the bitmap is freed.


Solution

  • Root Cause : So it appears that the image moves its top-left corner to the center of where the image was located in the TScrollBox when the bitmap is freed.

    Resolution : Move image to top after turning off scrollbars and freeing image, but before loading new image into image object.

    Code Sample..

    try
      // Reset existing images
      if assigned(Image1.Picture.Bitmap) then
        Image1.Picture.Bitmap.FreeImage; // using .Free eventually caused memory issues
        // .Free should only be in Finally code section for process objects
        // or on Destroy event for program objects
    
      Image1.Picture.Graphic := TBitmap.Create;
      Image1.Picture.Bitmap := TBitmap.Create;
    
      // reset Bitmap
      if assigned(bitmap123) then
        bitmap123.FreeImage;
    
      bitmap123 := TBitmap.Create;
    
    finally
      ScrollBox1.HorzScrollBar.Visible := false;
      ScrollBox1.VertScrollBar.Visible := false;
      Image1.Top := 0; Image1.Left := 0;
      Image1.Refresh;
      Application.ProcessMessages;
    
      ScrollBox1.HorzScrollBar.Visible := true;
      ScrollBox1.VertScrollBar.Visible := true;
      ScrollBox1.Refresh;
    
    end;
    // now images can be loaded 
    // and they will appear in the top-left corner of the scrollbox every time.