C#做图性能问题,大家一起来讨论

CraxyMouse 2008-02-24 10:17:29
我做了一个测试程序 。功能是在控件里跟鼠标绘二条正交的线
程序如下:
当我鼠标移动的快一些的时候,CPU占用率都在30%左右有什么办法可以降低CPU的占用率呢!?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Performance
{
public class Scale : Control
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region 组件设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}

#endregion

public Scale()
{
//enable double buffering of graphics
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
InitializeComponent();
}

Point p1 = new Point(0, 0), p2 = new Point(0, 0), p3 = new Point(0, 0), p4 = new Point(0, 0);
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawLine(Pens.Blue, p1, p2);
g.DrawLine(Pens.Green, p3, p4);
}

protected override void OnMouseMove(MouseEventArgs e)
{
p1 = new Point(0, e.Y);
p2 = new Point(this.Width, e.Y);
p3 = new Point(e.X, 0);
p4 = new Point(e.X, this.Height);
Invalidate();
}
}
}
...全文
126 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lixin19821010 2008-02-25
  • 打赏
  • 举报
回复
做图.net提供了GDI,还有相关,我也画过一些图.但是总感觉画出来的图很难看,形式上也比较单薄一点
比如office提供了一些控件图就画的很漂亮,3D的都很漂亮
还有www.codeproject.com提供的ZedGraph.dll画图就很漂亮,而且提供的功能也多,形式也很多样
以前我做过一个小项目就是ZedGraph.dll,画出来的图大家都觉得很满意:)
blestcc 2008-02-25
  • 打赏
  • 举报
回复
用.net就不要指望性能会好
kbryant 2008-02-25
  • 打赏
  • 举报
回复
进来学习了
qiyousyc 2008-02-25
  • 打赏
  • 举报
回复
想控制性能?不太可能的。
用别的开发图形吧。c#这方面还差很多。
春天的气息 2008-02-24
  • 打赏
  • 举报
回复
建议使用WPF作图吧,.net的性能让人哭,又哭不出来!
lnwuyaowei 2008-02-24
  • 打赏
  • 举报
回复
好象wpf作图性能还是不错的。
王集鹄 2008-02-24
  • 打赏
  • 举报
回复
别用Invalidate();重绘会快一些,参考如下代码:

public class Scale : Control
{
private System.ComponentModel.IContainer components = null;

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region 组件设计器生成的代码
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion

public Scale()
{
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
InitializeComponent();
}

Point p1 = new Point(0, 0), p2 = new Point(0, 0),
p3 = new Point(0, 0), p4 = new Point(0, 0);

private void DrawGraphic(Graphics g)
{
g.FillRectangle(new SolidBrush(BackColor), g.ClipBounds);
g.DrawLine(Pens.Blue, p1, p2);
g.DrawLine(Pens.Green, p3, p4);
}

protected override void OnPaint(PaintEventArgs e)
{
DrawGraphic(e.Graphics);
}

protected override void OnMouseMove(MouseEventArgs e)
{
p1 = new Point(0, e.Y);
p2 = new Point(Width, e.Y);
p3 = new Point(e.X, 0);
p4 = new Point(e.X, Height);
Graphics g = CreateGraphics();
DrawGraphic(g);
g.Dispose();
}
}
he_8134 2008-02-24
  • 打赏
  • 举报
回复
同3楼
vrhero 2008-02-24
  • 打赏
  • 举报
回复
别说GDI+了...只要是.NET我都不跟人讨论性能问题...
he_8134 2008-02-24
  • 打赏
  • 举报
回复
讨论是吧..托管代码GDI+慢 只是封装的好...想想微软的战略..为什么推出WPF..
CraxyMouse 2008-02-24
  • 打赏
  • 举报
回复

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧