我有一个TColor的二维数组。我也有一个TCanvas。如何在画布上绘制此颜色贴图的速度要快于 for 循环?
例如:
type
T2DAr = array of array of TColor;
var
ar: T2DAr;
Form1: TForm; // mainform
function main;
var x, y: integer;
begin
{filling array with colors as a 10x10}
for x := 0 to length(ar)-1 do
for y := 0 to length(ar[x])-1 do
Form1.Canvas.Pixels[x, y] := ar[x, y];
end;
这种方式效果太慢。我需要更快的算法。
答案 0 :(得分:4)
这已被多次回答。答案是:使用scanline
而不是非常慢的Pixels
属性。例如:
function CreateBitmapReallyFast: TBitmap;
const
WHITE: TRGBTriple = (rgbtBlue: 255; rgbtGreen: 255; rgbtRed: 255);
BLACK: TRGBTriple = (rgbtBlue: 0; rgbtGreen: 0; rgbtRed: 0);
var
y: Integer;
scanline: PRGBTriple;
x: Integer;
begin
result := TBitmap.Create;
result.SetSize(1920, 1080);
result.PixelFormat := pf24bit;
for y := 0 to result.Height - 1 do
begin
scanline := result.ScanLine[y];
for x := 0 to result.Width - 1 do
begin
if odd(x) then
scanline^ := WHITE
else
scanline^ := BLACK;
inc(scanline);
end;
end;
end;
更酷:
with scanline^ do
begin
rgbtBlue := Random(255);
rgbtGreen := Random(255);
rgbtRed := Random(255);
end;
试一试:
procedure TForm1.FormPaint(Sender: TObject);
var
bm: TBitmap;
begin
bm := CreateBitmapReallyFast;
try
Canvas.Draw(0, 0, bm);
finally
bm.Free;
end;
end;
当然,如果你有一个{packed 1}}或TRGBTriple
的(打包)数组,并且位图的像素格式相同,你只需要TRGBQuad
内存中的数据从数组到位图的扫描线。