FMX的DrawLine()函数为什么无法正常工作?

huangapple go评论58阅读模式
英文:

Why is FMX's DrawLine() function not working properly?

问题

I'm using C++Builder 11 Community Edition on a Windows 10 PC.

我正在使用C++Builder 11社区版在Windows 10 PC上。

I'm trying to draw lines. When I make a VCL app, it all works fine, but I want to use FMX to take advantage of the GPU.

我正在尝试绘制线条。当我创建一个VCL应用程序时,一切正常,但我想使用FMX来利用GPU的优势。

The FMX code is giving me blurred lines with Stroke->Thickness set to 1.0. I need the line thickness to be 1 pixel wide. This will eventually be a program that draws a bar graph.

FMX代码使我的线条模糊,Stroke->Thickness设置为1.0。我需要线条的厚度为1像素宽。最终这将是一个绘制条形图的程序。

This is the VCL code:

这是VCL代码:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    Canvas->Pen->Width = 1;
    Canvas->Pen->Color = (Graphics::TColor)clBlack;

    Canvas->MoveTo(100,100);
    Canvas->LineTo(500,100);
    Canvas->MoveTo(100,101);
    Canvas->LineTo(300,101);
    Canvas->MoveTo(100,102);
    Canvas->LineTo(500,102);
}

这是VCL代码:

FMX的DrawLine()函数为什么无法正常工作?

FMX的DrawLine()函数为什么无法正常工作?

This is the FMX code:

这是FMX代码:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    Canvas->BeginScene();
    // Canvas->Clear(TAlphaColorRec::Blue);

    Canvas->Stroke->Color = TAlphaColorRec::Black;
    Canvas->Stroke->Thickness = 1.0;
    Canvas->DrawLine(TPointF(100,100), TPointF(500,100), 1.0);
    Canvas->DrawLine(TPointF(100,101), TPointF(300,101), 1.0);
    Canvas->DrawLine(TPointF(100,102), TPointF(500,102), 1.0);

    Canvas->EndScene();
}

这是FMX代码:

FMX的DrawLine()函数为什么无法正常工作?

FMX的DrawLine()函数为什么无法正常工作?

I've tried different opacities and line thicknesses. If I repeat the line draw function 10 times, it looks better, but this is obviously not the way to go!

我尝试了不同的不透明度和线条厚度。如果我重复绘制线条函数10次,看起来会更好,但这显然不是正确的方法!

Can anybody help with this?

有人可以帮助吗?

英文:

I'm using C++Builder 11 Community Edition on a Windows 10 PC.

I'm trying to draw lines. When I make a VCL app, it all works fine, but I want to use FMX to take advantage of the GPU.

The FMX code is giving me blurred lines with Stroke->Thickness set to 1.0. I need the line thickness to be 1 pixel wide. This will eventually be a program that draws a bar graph.

This is the VCL code:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
	Canvas->Pen->Width = 1;
	Canvas->Pen->Color = (Graphics::TColor) clBlack;

	Canvas->MoveTo(100,100);
	Canvas->LineTo(500,100);
	Canvas->MoveTo(100,101);
	Canvas->LineTo(300,101);
	Canvas->MoveTo(100,102);
	Canvas->LineTo(500,102);
}

FMX的DrawLine()函数为什么无法正常工作?

This is the FMX code:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
	Canvas->BeginScene();
//	Canvas->Clear(TAlphaColorRec::Blue);

	Canvas->Stroke->Color = TAlphaColorRec::Black;
	Canvas->Stroke->Thickness = 1.0;
	Canvas->DrawLine(TPointF(100,100), TPointF(500,100), 1.0);
	Canvas->DrawLine(TPointF(100,101), TPointF(300,101), 1.0);
	Canvas->DrawLine(TPointF(100,102), TPointF(500,102), 1.0);

	Canvas->EndScene();
}

FMX的DrawLine()函数为什么无法正常工作?

I've tried different opacities and line thicknesses. If I repeat the line draw function 10 times, it looks better, but this is obviously not the way to go!

Can anybody help with this?

答案1

得分: 0

解决方案是将所有坐标偏移半个像素,以便从像素中心绘制,而不是在像素边界处绘制。这确保了抗锯齿图形不会混合像素在边界上。您还可以将Form的Quality属性设置为HighPerformance以禁用抗锯齿,但这可能会对表单的其他部分的图形质量产生负面影响,您可能希望保持抗锯齿。

以下代码在Quality设置为HighQuality时有效:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    Canvas->BeginScene();
    //  Canvas->Clear(TAlphaColorRec::Blue);
    
    Canvas->Stroke->Color = TAlphaColorRec::Black;
    Canvas->Stroke->Thickness = 1.0;
    Canvas->DrawLine(TPointF(100.5,100.5), TPointF(500.5,100.5), 1.0);
    Canvas->DrawLine(TPointF(100.5,101.5), TPointF(300.5,101.5), 1.0);
    Canvas->DrawLine(TPointF(100.5,102.5), TPointF(500.5,102.5), 1.0);
    
    Canvas->EndScene();
}
英文:

The solution is to offset all coordinates by half a pixel so that it's drawing from the center of the pixel rather than at a pixel boundary. This ensures that the anti-aliased graphics doesn't blend the pixels over a boundary. You could also set the Form's Quality property to HighPerformance to disable anti-aliasing, but that may negatively affect graphics quality on other parts of the form where you may want to keep anti-aliasing.

The following works with Quality set to HighQuality

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    Canvas->BeginScene();
//  Canvas->Clear(TAlphaColorRec::Blue);

    Canvas->Stroke->Color = TAlphaColorRec::Black;
    Canvas->Stroke->Thickness = 1.0;
    Canvas->DrawLine(TPointF(100.5,100.5), TPointF(500.5,100.5), 1.0);
    Canvas->DrawLine(TPointF(100.5,101.5), TPointF(300.5,101.5), 1.0);
    Canvas->DrawLine(TPointF(100.5,102.5), TPointF(500.5,102.5), 1.0);

    Canvas->EndScene();
}

huangapple
  • 本文由 发表于 2023年5月10日 18:42:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定