将拉伸后的图像添加到Delphi中的ImageList中 [英] Add stretched image to ImageList in Delphi

查看:281
本文介绍了将拉伸后的图像添加到Delphi中的ImageList中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,在图片"字段中包含图像",然后将它们放入ImageList中. 这是代码:

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;

但是对于与ImageList大小不同的图像,我有一些问题.

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

更新: 我的问题是,当添加大于ImageList大小(32 * 32)的图像时,例如100 * 150,它在连接到ImageList的组件中(例如在ListView中)无法正确显示. 似乎新添加的图像未拉伸,但已裁剪.我希望像在ImageList编辑器中一样拉伸新图像.

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.

推荐答案

我不知道ImageList是否提供自动拉伸图像的属性.除非有人找到内置文件,否则您始终可以自行拉伸图像,然后再将其添加到ImageList中.当您使用它时,请停止使用磁盘上的文件:改为使用TMemoryStream.像这样:

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:我在浏览器窗口中编辑了您的代码.我不能保证它可以编译,但是如果不能编译,应该很容易修复.


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.

这篇关于将拉伸后的图像添加到Delphi中的ImageList中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆