Board index » delphi » resizing TBitmap to fit in TImageList

resizing TBitmap to fit in TImageList

    When adding bitmaps to ImageList in design time, and the bitmap is
bigger than ImageList dimensions, you get asked to split the image into x
separate images. If you answer no, the bitmap however is still properly
resized before it is placed in the list. How is this done programatically ?
ImageList1.Add (Bitmap, nil) will only store the bitmap up to the width and
height specified by ImageList, but not resize it, so only a part of the
original bitmap is stored.

The two possible solutions I guess would be telling ImageList to resize the
bitmap before storing it or resizing the bitmap "by hand" and then adding it
to the list. Is either solution possible ?

Thanks,

Matej

 

Re:resizing TBitmap to fit in TImageList


Sorry, a little mistake: actually the ImageList won't accept bitmaps (an
exception is raised) of incorrect size at all.

So I had to set bitmap's width and height to that of the imagelist which
caused the rest of the image to be cut off.

Matej

Re:resizing TBitmap to fit in TImageList


Matej,

Quote
> Sorry, a little mistake: actually the ImageList won't accept bitmaps (an
> exception is raised) of incorrect size at all.

Well since TImageList is a collection of same-sized images you'll just
have to make sure all the newly inserted bitmaps are equal the Height
and Width of the imagelist.

Quote
> So I had to set bitmap's width and height to that of the imagelist which
> caused the rest of the image to be cut off.

You got that part, but go just a bit further. You had mentioned if
resizing the bitmap "by hand" and then adding it to the list is
possible? Of course, this requires a bit of work on your part. How about
something like...

...
var
  Bitmap: TBitmap;
begin
  Bitmap := TBitmap.Create;
  try
    // Set H&W of Bitmap to match ImageList
    Bitmap.Height := ImageList1.Height;
    Bitmap.Width := ImageList1.Width;

    // StretchDraw Bitmap with some bitmap, in the case
Image1.Picture.Bitmap
    with Bitmap do
      Canvas.StretchDraw(Rect(0, 0, Width, Height),
Image1.Picture.Bitmap);  

    // Add Resized/Scaled bitmap to image list
    Imagelist1.Add(Bitmap, nil);
  finally
    Bitmap.Free;
  end;
end;

HTH,

Robert

Other Threads