在Delphi中将拉伸图像添加到ImageList

发布于 2024-11-24 04:08:42 字数 640 浏览 0 评论 0原文

我有一个表,其中包含图片字段中的图像,我将把它们放入 ImageList 中。 这是代码:

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 尺寸不同的图像,我遇到了一些问题。

更新: 我的问题是,当添加大于 ImageList 大小(32 * 32)的图像时,例如 100 * 150 它不会正确显示在连接到 ImageList 的组件中(例如在 ListView 中)。 看来新添加的图像没有被拉伸,而是被裁剪了。我希望像在 ImageList Editor 中一样拉伸新图像。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

非要怀念 2024-12-01 04:08:42

不知道ImageList是否提供了自动拉伸图像的属性。除非有人找到一些内置的,否则您始终可以在将图像添加到 ImageList 之前自行拉伸图像。当您这样做时,请停止使用磁盘上的文件:改用TMemoryStream。像这样的:

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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文