Add stretched image to ImageList in Delphi

眉间皱痕 提交于 2020-01-12 09:18:09

问题


I have a table contains Image in a Picture field and I am going to put them into an ImageList. Here is the code:

ImageList.Clear;
ItemsDts.First;
ImageBitmap:= TBitmap.Create;
try
  while not ItemsDts.Eof do
  begin
    if not ItemsDtsPicture.IsNull then
    begin
      ItemsDtsPicture.SaveToFile(TempFileBitmap);
      ImageBitmap.LoadFromFile(TempFileBitmap);
      ImageList.Add(ImageBitmap, nil);
    end;
    ItemsDts.Next;
  end;
finally
  ImageBitmap.Free;
end;

But I have some problem for images with difference size from ImageList size.

Update: My problem is that when adding Image larger than ImageList size (32 * 32), for example 100 * 150 It does not appear correctly in a component connected to ImageList (for example in a ListView). It seems newly added image is not stretched but is Croped. I want new image to be stretched as in ImageList Editor.


回答1:


I don't know if ImageList provides a property to automatically stretch the image. Unless someone finds some built-in, you can always stretch the image yourself before adding it to the ImageList. And while you're at it, stop using the file-on-disk: use a TMemoryStream instead. Something like this:

var StretchedBMP: TBitmap;
    MS: TMemoryStream;

ImageList.Clear;
ItemsDts.First;
StretchedBMP := TBitmap.Create;
try

  // Prepare the stretched bmp's size
  StretchedBMP.Width := ImageList.Width;
  StretchedBMP.Height := ImageList.Height;

  // Prepare the memory stream
  MS := TMemoryStream.Create;
  try
    ImageBitmap:= TBitmap.Create;
    try
      while not ItemsDts.Eof do
      begin
        if not ItemsDtsPicture.IsNull then
        begin
          MS.Size := 0;
          ItemsDtsPicture.SaveToStream(MS);
          MS.Position := 0;
          ImageBitmap.LoadFromStream(MS);
          // Stretch the image
          StretchedBMP.Canvas.StretchDraw(Rect(0, 0, StretchedBmp.Width-1, StretchedBmp.Height-1), ImageBitmap);
          ImageList.Add(StretchedBmp, nil);
        end;
        ItemsDts.Next;
      end;
    finally MS.Free;
    end;
  finally StretchedBMP.Free;
  end;
finally
  ImageBitmap.Free;
end;

PS: I edited your code in the browser's window. I can't guarantee it compiles, but if it doesn't, it should be easy to fix.



来源:https://stackoverflow.com/questions/6675841/add-stretched-image-to-imagelist-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!