编写程序对GDI和GDI+绘制进行了比较,经过比较,GDI相对GDI+还是有一些性能优势的。
同时比较了每次绘制创建TGPGraphics对象和共用一个TGPGraphics对象的情况,两者性能相差不大,几可忽略。
1.用GDI绘制5K次----耗时约为19s200ms
procedure TForm8.WMPaint(var Message: TWMPaint); var ps: PAINTSTRUCT; LClientRect: TGPRect; LGraph: TGPGraphics; LBrush: TGPBrush; LBr: HGDIOBJ; begin if m_nCount < 5000 then begin //创建缓存 BeginPaint(Handle, ps); if not Assigned(m_memDC) then m_memDC := TMemoryDC.Create(ps.hdc); m_memDC.SetBounds(ps.hdc, Self.ClientRect); //用GDI+绘制 // LBrush := TGPSolidBrush.Create(aclRed); // LClientRect := MakeRect(Self.ClientRect); // m_memDC.Graph.FillRectangle(LBrush, LClientRect); // LBrush.Free; //用GDI绘制 LBr := CreateSolidBrush(clRed); FillRect(m_memDC.DC, Self.ClientRect, LBr); DeleteObject(LBr); //缓冲去拷贝 m_memDC.Blt(ps.hdc); EndPaint(Handle, ps); Message.Result := 0; Inc(m_nCount); end else begin BeginPaint(Handle, ps); EndPaint(Handle, ps); Message.Result := 0; end; end;
2.用GDI+绘制5K次----耗时约为19s600ms
procedure TForm8.WMPaint(var Message: TWMPaint); var ps: PAINTSTRUCT; LClientRect: TGPRect; LGraph: TGPGraphics; LBrush: TGPBrush; LBr: HGDIOBJ; begin if m_nCount < 5000 then begin //创建缓存 BeginPaint(Handle, ps); if not Assigned(m_memDC) then m_memDC := TMemoryDC.Create(ps.hdc); m_memDC.SetBounds(ps.hdc, Self.ClientRect); //用GDI+绘制 LGraph := TGPGraphics.Create(m_memDC.DC); LBrush := TGPSolidBrush.Create(aclRed); LClientRect := MakeRect(Self.ClientRect); LGraph.FillRectangle(LBrush, LClientRect); LGraph.Free; LBrush.Free; //用GDI绘制 // LBr := CreateSolidBrush(clRed); // FillRect(m_memDC.DC, Self.ClientRect, LBr); // DeleteObject(LBr); //缓冲去拷贝 m_memDC.Blt(ps.hdc); EndPaint(Handle, ps); Message.Result := 0; Inc(m_nCount); end else begin BeginPaint(Handle, ps); EndPaint(Handle, ps); Message.Result := 0; end; end;
3.用GDI+绘制5K次(不重复创建TGPGraphics)----耗时约为19s500ms
procedure TForm8.WMPaint(var Message: TWMPaint); var ps: PAINTSTRUCT; LClientRect: TGPRect; LGraph: TGPGraphics; LBrush: TGPBrush; LBr: HGDIOBJ; begin if m_nCount < 5000 then begin //创建缓存 BeginPaint(Handle, ps); if not Assigned(m_memDC) then m_memDC := TMemoryDC.Create(ps.hdc); m_memDC.SetBounds(ps.hdc, Self.ClientRect); //用GDI+绘制 LBrush := TGPSolidBrush.Create(aclRed); LClientRect := MakeRect(Self.ClientRect); m_memDC.Graph.FillRectangle(LBrush, LClientRect); LBrush.Free; //用GDI绘制 // LBr := CreateSolidBrush(clRed); // FillRect(m_memDC.DC, Self.ClientRect, LBr); // DeleteObject(LBr); //缓冲去拷贝 m_memDC.Blt(ps.hdc); EndPaint(Handle, ps); Message.Result := 0; Inc(m_nCount); end else begin BeginPaint(Handle, ps); EndPaint(Handle, ps); Message.Result := 0; end; end;