在FireMonkey中绘制像素的最快方式 [英] Fastest way to draw pixels in FireMonkey
问题描述
我已经做了以下代码:
procedure TForm15.Button1Click(Sender: TObject);
var
Bitmap1: TBitmap;
im: TImageControl;
Color: TColor;
Scanline: PAlphaColorArray;
x,y,i: Integer;
begin
for i:= 1 to 100 do begin
im:= ImageControl1;
Bitmap1:= TBitmap.Create(100,100);
try
for y:= 0 to 99 do begin
ScanLine:= Bitmap1.ScanLine[y];
for x:= 0 to 99 do begin
ScanLine[x]:= Random(MaxInt);
end;
end;
ImageControl1.Canvas.BeginScene;
ImageControl1.Canvas.DrawBitmap(Bitmap1, RectF(0,0,Bitmap1.Width, Bitmap1.Height)
,im.ParentedRect,1,true);
ImageControl1.Canvas.EndScene;
finally
Bitmap1.Free;
end;
end;
end;
有更快的方式在Firemonkey中绘制像素吗?
我的目标是使用康威的生活游戏制作演示程序。
Is there a faster way to draw pixels in Firemonkey?
I aim to make a demo program using Conway's game of life.
推荐答案
所有的时间都花在执行这两行代码:
All the time is spent performing these two lines of code:
ImageControl1.Canvas.BeginScene;
ImageControl1.Canvas.EndScene;
您可以删除所有与位图一起使用的代码和实际绘制位图的代码使运行时不会有一个差异。换句话说,瓶颈是场景代码不是位图代码。我没有办法优化这个。
You can delete all the code that works with the bitmap and the code that actually draws the bitmap and it makes not one iota of difference to the runtime. In other words, the bottleneck is the scene code not the bitmap code. And I see no way for you to optimise that.
我的测试代码如下所示:
My test code looked like this:
Stopwatch := TStopwatch.StartNew;
for i:= 1 to 100 do begin
ImageControl1.Canvas.BeginScene;
ImageControl1.Canvas.EndScene;
end;
ShowMessage(IntToStr(Stopwatch.ElapsedMilliseconds));
与您的代码的时间相同,在我的机器上为1600ms。如果您删除 BeginScene
, DrawBitmap
和 EndScene
调用您的代码在我的机器上运行3ms。
This has the same elapsed time as your code, 1600ms on my machine. If you remove the BeginScene
, DrawBitmap
and EndScene
calls then your code runs in 3ms on my machine.
这篇关于在FireMonkey中绘制像素的最快方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!