GDI與GDI+性能比較


編寫程序對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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM