简体   繁体   English

如何在TImage中平铺图像?

[英]How to tile a Image in TImage?

How do I tile an image in a TImage in Delphi? 如何在Delphi中的TImage中平铺图像?

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. 为什么我需要它 :我可以在运行时创建一个并存储我的图像,而不是在运行时创建更多的TImages,因为它知道它将“适合”直到达到TImage的高度和宽度。

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. 编辑:请注意,我不是要求拉伸图像,而是通过重复图像填充画布。

The following is the function that I have used, taking an existing TImage component and tiling it over a target canvas: 以下是我使用的函数,使用现有的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;

Because a tLabel exposes a canvas, you can do tricks like the following: 因为tLabel公开了一个画布,你可以做如下的技巧:

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

Assuming your image is a bitmap and loaded into the TImage you can use the following 假设您的图像是位图并加载到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;

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. Image1.Canvas和Image1.Picture.Bitmap.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". 如果您尝试将TImage中的位图分配给画笔而不将其分配给另一个位图对象,则首先像Image1.Canvas.Brush.Bitmap:= Image1.Picture.Bitmap,您将获得“没有足够的存储空间”的异常。

You could set the canvas.brush.bitmap := to the image of the tile. 您可以将canvas.brush.bitmap :=设置为canvas.brush.bitmap :=的图像。 then canvas.fillrect(canvas.cliprect) to tile the whole canvas with the selected tile image. 然后使用canvas.fillrect(canvas.cliprect)使用选定的平铺图像平铺整个画布。 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. 我没有在很长一段时间内完成它,我现在无法检查这是否真的是在Delphi中完成的,但我很确定这就是你所追求的。

Delphi installation comes with a Demo named 'Bitmap' (you can find the project in Help dir.). Delphi安装附带一个名为“Bitmap”的Demo(您可以在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! 希望有所帮助!

By "fitting" do you mean "tiling"? 通过“拟合”你的意思是“平铺”? As far as I know, TImage does not support this out of the box. 据我所知,TImage不支持开箱即用。 You'd have to manually draw your picture on the TImage's Canvas in a repeating pattern. 您必须以重复模式在TImage的画布上手动绘制图片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM