to speed up painting a bitmap in Delphi XE2, I decided to go the following way
a) Create a eg. 10 x Thread and paint only one layer of the bitmap inside the thread class b) once all threads a finished, merge the bitmaps using the bitblt function layer by layer
I did the following experiemental code
unit Unit_BitmapThread;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Data.DB, Data.Win.ADODB, ActiveX;
type PaintBitmapThread = class(TThread)
private
FBitmap : TBitmap;
FConnection : TAdoConnection;
fserver, fdatabasename, ftablename, fsqlstr : String;
protected
procedure Execute; override;
public
constructor Create ( bmp_width, bmp_height : Integer; server, databasename, tablename, sqlstr : String; ThreadId : Integer );
destructor destroy ; override;
end;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
FAdoConnection : TAdoConnection;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var i : Integer;
aPaintBitmapThread : PaintBitmapThread;
begin
for i := 1 to 10 do
begin
aPaintBitmapThread :=PaintBitmapThread.Create(100,100,'server','database','table','select *',1);
end;
end;
{ PaintBitmapThread }
constructor PaintBitmapThread.Create(bmp_width, bmp_height: Integer;
server, databasename, tablename, sqlstr: String; ThreadId: Integer);
begin
FBitmap :=TBitmap.create;
FConnection :=TAdoConnection.Create(nil);
end;
destructor PaintBitmapThread.destroy;
begin
FBitmap.Free;
FConnection.Free;
inherited;
end;
procedure PaintBitmapThread.Execute;
var
ThreadQuery : TADOQuery;
k : integer;
begin
inherited;
CoInitialize(nil) ; //CoInitialize was not called
ThreadQuery := TADOQuery.Create(nil) ;
try
// ADO DB THREAD MUST USE OWN CONNECTION
ThreadQuery.Connection := FConnection;
// ThreadQuery.ConnectionString := '????';
// ThreadQuery.CursorLocation := clUseServer;
// ThreadQuery.LockType := ltReadOnly;
// ThreadQuery.CursorType := ctOpenForwardOnly;
ThreadQuery.SQL.Text := FSQLStr;
// ThreadQuery.Open;
while NOT ThreadQuery.Eof and NOT Terminated do
begin
//Canvas Does NOT Allow Drawing if not called through Synchronize
//Synchronize(RefreshCount) ;
ThreadQuery.Next;
end;
finally
ThreadQuery.Free;
end;
CoUninitialize()
end;
end.
Q : a) How to detect once all 10 painting threads are finished and b) How to access the thread[i] and get the bitmap to the main program (VCL) for merging ?
Best solution would be
if Thread[1] and Thread[2] finished -> newBMP := Bitblt( bmp1, bm2);
if also Thread[3]finished -> newBMP := Bitblt( newBMP, bm3);
unit all threads-Bitmaps are merged