如何在 TImage 中平铺图像?

发布于 2024-07-30 19:24:49 字数 223 浏览 5 评论 0原文

如何在 Delphi 中平铺 TImage 中的图像?

为什么我需要它:我可以创建一个并将图像存储在那里,而不是在运行时创建更多 TImage,因为我知道它将“适合”直到达到 TImage 的高度和宽度。

请提出任何想法来做到这一点。

谢谢你!

编辑:请注意,我不是要求拉伸图像,而是通过重复图像来填充画布。

How do I tile an image in a TImage in Delphi?

Why I need it: Instead of creating more TImages at runtime, I could create one and store my image there knowing that it will be 'fit' until it reaches TImage's height and width.

Please suggest any ideas to do this.

Thank you!

EDIT: Please note, I'm not asking for streching the image, but filling the canvas by repeating the image.

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

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

发布评论

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

评论(5

我的奇迹 2024-08-06 19:24:49

假设您的图像是位图并加载到 TImage 中,您可以使用以下

procedure TmyForm.Button1Click(Sender: TObject);
    var mybmp:TBitmap;
begin
    mybmp:= TBitmap.Create();
    try
        mybmp.Assign(Image1.Picture.Bitmap);

        Image1.Picture.Bitmap.SetSize(Image1.Width,Image1.Height);
        Image1.Canvas.Brush.Bitmap := mybmp;
        Image1.Canvas.FillRect(Image1.BoundsRect);

        mybmp.FreeImage;
    finally
        FreeandNil(mybmp)
    end;
end;

一些注释

如果在给图像添加标题后保存图像,您将保存带标题的版本而不是原始版本。

Image1.Canvas 和 Image1.Picture.Bitmap.Canvas 是相同的,这就是为什么您需要在画布上绘画之前调整位图的大小。

如果您尝试将 TImage 中的位图分配给画笔,而不先将其分配给另一个位图对象,就像这样 Image1.Canvas.Brush.Bitmap := Image1.Picture.Bitmap 你会得到一个异常“not足够的存储空间”。

Assuming your image is a bitmap and loaded into the TImage you can use the following

procedure TmyForm.Button1Click(Sender: TObject);
    var mybmp:TBitmap;
begin
    mybmp:= TBitmap.Create();
    try
        mybmp.Assign(Image1.Picture.Bitmap);

        Image1.Picture.Bitmap.SetSize(Image1.Width,Image1.Height);
        Image1.Canvas.Brush.Bitmap := mybmp;
        Image1.Canvas.FillRect(Image1.BoundsRect);

        mybmp.FreeImage;
    finally
        FreeandNil(mybmp)
    end;
end;

Some notes:

If you save the image after titling it you will save the titled version not the original.

Image1.Canvas and Image1.Picture.Bitmap.Canvas are one and the same, that's why you need to resize the bitmap before painting on the canvas.

If you try and assign the bitmap in the TImage to the brush without assigning it to another bitmap object first like so Image1.Canvas.Brush.Bitmap := Image1.Picture.Bitmap you get an exception "not enough storage".

一江春梦 2024-08-06 19:24:49

Delphi 安装附带一个名为“Bitmap”的 Demo(您可以在 Help 目录中找到该项目)。

它使用以下方法来绘制平铺图像:

procedure TBmpForm.FormPaint(Sender: TObject);
var
  x, y: Integer;
begin
  y := 0;
  while y < Height do
  begin
    x := 0;
    while x < Width do
    begin
      // Bitmap is a TBitmap.
      //  form's OnCreate looks like this:
      //    Bitmap := TBitmap.Create;
      //    Bitmap.LoadFromFile('bor6.bmp');
      //  or you can use Canvas.Draw(x, y, Image1.Picture.Bitmap),
      //  instead of Canvas.Draw(x, y, Bitmap);
      //
      Canvas.Draw(x, y, Bitmap); //Bitmap is a TBitmap. 
      x := x + Bitmap.Width; // Image1.Picture.Bitmap.Width;
    end;
    y := y + Bitmap.Height; // Image1.Picture.Bitmap.Height;
  end;
end;

希望有帮助!

Delphi installation comes with a Demo named 'Bitmap' (you can find the project in Help dir.).

It uses the following method to draw a tiled image:

procedure TBmpForm.FormPaint(Sender: TObject);
var
  x, y: Integer;
begin
  y := 0;
  while y < Height do
  begin
    x := 0;
    while x < Width do
    begin
      // Bitmap is a TBitmap.
      //  form's OnCreate looks like this:
      //    Bitmap := TBitmap.Create;
      //    Bitmap.LoadFromFile('bor6.bmp');
      //  or you can use Canvas.Draw(x, y, Image1.Picture.Bitmap),
      //  instead of Canvas.Draw(x, y, Bitmap);
      //
      Canvas.Draw(x, y, Bitmap); //Bitmap is a TBitmap. 
      x := x + Bitmap.Width; // Image1.Picture.Bitmap.Width;
    end;
    y := y + Bitmap.Height; // Image1.Picture.Bitmap.Height;
  end;
end;

Hope that helps!

节枝 2024-08-06 19:24:49

您可以将 canvas.brush.bitmap := 设置为图块的图像。 然后 canvas.fillrect(canvas.cliprect) 使用所选平铺图像平铺整个画布。 我已经很长时间没有这样做了,我无法检查这是否真的是现在在 Delphi 中完成的,但我很确定这就是你想要的。

You could set the canvas.brush.bitmap := to the image of the tile. then canvas.fillrect(canvas.cliprect) to tile the whole canvas with the selected tile image. I haven't done it in a long time and I am not able to check if this is really how it's done in Delphi right now, but I am pretty sure this is what you're after.

旧时模样 2024-08-06 19:24:49

以下是我使用的函数,采用现有的 TImage 组件并将其平铺在目标画布上:

procedure TileImage(const Source:tImage;
    Target: TCanvas;
    TargetHeight,TargetWidth:integer);
// Tiles the source image over the given target canvas
var
  X, Y: Integer;
  dX, dY: Integer;
begin
  dX := Source.Width;
  dY := Source.Height;
  Y := 0;
  while Y < TargetHeight do
    begin
      X := 0;
      while X < TargetWidth do
        begin
          Target.Draw(X, Y, Source.Picture.graphic);
          Inc(X, dX);
        end;
      Inc(Y, dY);
    end;
end;

因为 tLabel 公开画布,所以您可以执行如下操作:

TileImage(Image1,Label1.Canvas,Label1.Height,Label1.Width);

The following is the function that I have used, taking an existing TImage component and tiling it over a target canvas:

procedure TileImage(const Source:tImage;
    Target: TCanvas;
    TargetHeight,TargetWidth:integer);
// Tiles the source image over the given target canvas
var
  X, Y: Integer;
  dX, dY: Integer;
begin
  dX := Source.Width;
  dY := Source.Height;
  Y := 0;
  while Y < TargetHeight do
    begin
      X := 0;
      while X < TargetWidth do
        begin
          Target.Draw(X, Y, Source.Picture.graphic);
          Inc(X, dX);
        end;
      Inc(Y, dY);
    end;
end;

Because a tLabel exposes a canvas, you can do tricks like the following:

TileImage(Image1,Label1.Canvas,Label1.Height,Label1.Width);
自控 2024-08-06 19:24:49

你说的“装修”是指“铺瓷砖”吗?
据我所知,TImage 不支持开箱即用。 您必须以重复模式在 TImage 的画布上手动绘制图片。

By "fitting" do you mean "tiling"?
As far as I know, TImage does not support this out of the box. You'd have to manually draw your picture on the TImage's Canvas in a repeating pattern.

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