图像画布绘制最终在表单上结束 [英] Image canvas drawing ends up on form instead

查看:203
本文介绍了图像画布绘制最终在表单上结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在TImage的画布上绘制一些自定义形状,但输出最终会在TImage父窗体的画布上结束.看来我必须将我的观点从局部转换为绝对,才能完成这项工作,但这也引起了问题.参见示例:

I'm trying to draw some custom shapes on the canvas of a TImage, but the output ends up on the TImage parent form's canvas instead. It appears I have to convert my points from local to absolute to make this work, but that causes problems too. See example:

  • 有人知道为什么Image.Canvas.DrawArc(etc)相对于父窗体而不是相对于Image绘制吗?
  • 如果遇到.LocalToAbsolute的麻烦...为什么弧看起来如此不同?

该项目很简单:Firemonkey HD表单,中间是TPanel,而在TPanel内部是TImage(与客户端对齐).该按钮仅执行代码.

The project is simple: Firemonkey HD form with TPanel in the middle and TImage inside the TPanel (aligned to client). The button just executes the code.

代码如下:

    unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 
  FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
  FMX.Objects;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Panel1: TPanel;
    Image1: TImage;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}


procedure TForm1.Button1Click(Sender: TObject);
begin
  Image1.Canvas.BeginScene;

  {Trying to draw arc and line on Image's canvas - Doesn't work; it draws to Form's canvas instead}
  Image1.Canvas.DrawArc(PointF(0,0), PointF(10, 10), 0.0, 45.0, 1.0);
  Image1.Canvas.DrawLine(PointF(0.0, 0.0), PointF(100, 100), 1.0);

  {Trying to draw arc and line on Image's canvas - This works; by why should such steps be necessary and why is the arc so different?}
  Image1.Canvas.DrawArc(Image1.LocalToAbsolute(PointF(0,0)), Image1.LocalToAbsolute(PointF(10, 10)), 0.0, 45.0, 1.0);
  Image1.Canvas.DrawLine(Image1.LocalToAbsolute(PointF(0.0, 0.0)), Image1.LocalToAbsolute(PointF(100, 100)), 1.0);

  Image1.Canvas.EndScene;
end;

end.

推荐答案

我对此也遇到了一些问题,您应该使用@LHristov所说的位图.唯一的是,有一个错误无法正确设置位图的大小,其修复方法如下:

Well i've had some problems with this too and you should use a Bitmap as @LHristov said. The only thing is that there is a bug that doesn't set the bitmap size correctly and the fix for this is as follows:

procedure TForm3.FormCreate(Sender: TObject);
begin
  bitmap := TBitmap.Create;
  bitmap.SetSize(round(Image1.Width), round(Image1.Height));
  Image1.MultiResBitmap.Bitmaps[1].Assign(bit);
  Image1.Bitmap := Image1.MultiResBitmap.Bitmaps[1];
  Image1.Bitmap.Clear(TAlphaColorRec.White);
end;

procedure TForm3.Button1Click(Sender: TObject);
begin
  Image1.Bitmap.Canvas.BeginScene;

  Image1.Bitmap.Canvas.DrawArc(PointF(0,0), PointF(10, 10), 0.0, 145.0, 1.0);
  Image1.Bitmap.Canvas.DrawLine(PointF(0.0, 0.0), PointF(200, 200), 1.0);

  Image1.Bitmap.Canvas.DrawArc(Image1.LocalToAbsolute(PointF(0,0)), Image1.LocalToAbsolute(PointF(10, 10)), 0.0, 45.0, 1.0);
  Image1.Bitmap.Canvas.DrawLine(Image1.LocalToAbsolute(PointF(0.0, 0.0)), Image1.LocalToAbsolute(PointF(100, 100)), 1.0);

  Image1.Bitmap.Canvas.EndScene;

end;

希望这可以为您解决!

这篇关于图像画布绘制最终在表单上结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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