GDI与GDI+性能比较

Posted GS

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;

 

以上是关于GDI与GDI+性能比较的主要内容,如果未能解决你的问题,请参考以下文章

GDI+下Bitmap逐像素快速读写性能测试

(原创)[C#] GDI+ 之鼠标交互:原理示例一步步深入性能优化

GDI+学习---1.初识GDI+

《C# GDI+ 破境之道》:第一境 GDI+基础 —— 第三节:画圆形

使用GDI+生成KnownColor列表

VC++使用GDI+实现HBITMAP与图片文件之间的相互转换(附源码)