程序cpu占用太高 我编写了一个简单的制图程序,实现以下功能:图片的放大缩小,移动,动画功能(未完全实现),在调试时发现,当在程序的图形编辑面板里放上大约接近30个图的时候,cpu的占用接近100%,请问有什么改善的办法. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用多线程;但记得在线程的循环体内加上Thread.Sleep(100); 但记得在线程的循环体内加上Thread.Sleep(100);--------这是为何呢? 代码比较长,所以一开始没有贴出.怕贴出也说明不了什么.我的代码是参考http://www.codeproject.com/csharp/DrawTools.asp而做的,可以说没有什么大的改变.这里有中文翻译:http://wdxinren.cnblogs.com/archive/2004/10/19/54231.aspx我自定义了一个DrawIcon类继承自DrawObject,其中绘画方法实现如下:public override void Draw(Graphics g) { //画动画 //Begin the animation. AnimateImage(); //Get the next frame ready for rendering. ImageAnimator.UpdateFrames(); //画图标 Rectangle iconR = GetNormalizedRectangle(iconRect.X, iconRect.Y, iconRect.X + iconRect.Width, iconRect.Y + iconRect.Height); g.DrawImage(this.Image, iconR); //画文本 Brush brush = Brushes.Black; Font font = new Font("Arial", txtHeight - 3, FontStyle.Regular, GraphicsUnit.Pixel); StringFormat flags = new StringFormat(StringFormatFlags.DisplayFormatControl); RectangleF txtRF = new RectangleF(Convert.ToSingle(iconR.X), Convert.ToSingle(iconR.Bottom), Convert.ToSingle(iconR.Width), Convert.ToSingle(txtHeight)); g.DrawString(Text, font, brush, txtRF,flags);//画字符串 g.DrawRectangle(Pens.Black, Rectangle.Round(txtRF));//画框 }public void AnimateImage() { if (!currentlyAnimating) { //Begin the animation only once. ImageAnimator.Animate(image, new EventHandler(this.OnFrameChanged)); currentlyAnimating = true; } } /// <summary> /// 动画有关 /// </summary> private void OnFrameChanged(object o, EventArgs e) { //Force a call to the Paint event handler.//在类里没有发现使自己的实例无效重画的方法,所以动画功能没有实现 } 相关属性和方法:private Rectangle iconRect;//图标矩形(不包括文本)public DrawIcon(Bitmap iconImage, string iconText, int x, int y)//构造函数 { image = iconImage; Text = iconText; SetIcon(x, y, 16, 16); }/// <summary> /// 设置图标的大小,位置等 /// </summary> protected void SetIcon(int x, int y, int width, int height) { iconRect.X = x; iconRect.Y = y; iconRect.Width = width; iconRect.Height = height; } 然后....再实现GraphicsList类,把所有生成的DrawObject对象添加到这个集合里,以后循环这个集合里的Draw方法就可以实现对所有画板上的对象重绘了.最后实现出来的效果就是一个动画(初始16x16大小),动画下面是一行文本,能放大缩小,移动.不过目前动画还没能实现.....不知道我贴出的代码能否说明问题,如果还有什么需要请回贴. 不知道csdn有回帖子限制,又不能修改写过的内容,只好重新注册一个马甲,帖出一些其他问题:to JasonHeung(拥有一切不过就这样笑着哭) :你说的方法我也想过,但是这是我第一次图形编程.如何把多线程和我现在的程序结合起来,可否具体指点一下?或者给予相关例子也可以.不胜感激.to zhgroup(Hotel California) :如何判断该区域是否无效,可否详细一些?to wcmj(望尘莫及) :不是必须放30个.现实可能会少些,也可能多过30个,说不准.编程当然不能只关心功能的实现,还应该考虑性能问题.程序是随着放的图的增多,cpu占用逐渐增加,大约在30个左右的时候接近100% 大家好,请教一个关于c#中Convert的问题 c#做的一个访问页面访问速度很慢 模仿查询分析器,动态执行SQL语句 求助 有关 new listitem 什么是正则表达式 C#的程序用什么加壳呀? 200分问IT?谁主沉浮!不是我年少轻狂,而是我在网海里浮沉了一个星期,也没有找到船!DataGrid嵌套DataGrid??? 怎麼樣在C# windows Form中實現右鍵菜單 c# 中 多线程与事件处理器协同工作的问题?????? 解密之后信息带有乱字符,问题在哪里? 关于GridView模版列中控件绑定值后的取行问题 求助 图象处理高手进来帮下忙,急
--------这是为何呢?
我的代码是参考http://www.codeproject.com/csharp/DrawTools.asp而做的,可以说没有什么大的改变.这里有中文翻译:http://wdxinren.cnblogs.com/archive/2004/10/19/54231.aspx我自定义了一个DrawIcon类继承自DrawObject,其中绘画方法实现如下:
public override void Draw(Graphics g)
{
//画动画
//Begin the animation.
AnimateImage(); //Get the next frame ready for rendering.
ImageAnimator.UpdateFrames();
//画图标
Rectangle iconR = GetNormalizedRectangle(iconRect.X, iconRect.Y, iconRect.X + iconRect.Width, iconRect.Y + iconRect.Height);
g.DrawImage(this.Image, iconR); //画文本
Brush brush = Brushes.Black;
Font font = new Font("Arial", txtHeight - 3, FontStyle.Regular, GraphicsUnit.Pixel);
StringFormat flags = new StringFormat(StringFormatFlags.DisplayFormatControl);
RectangleF txtRF = new RectangleF(Convert.ToSingle(iconR.X), Convert.ToSingle(iconR.Bottom), Convert.ToSingle(iconR.Width), Convert.ToSingle(txtHeight));
g.DrawString(Text, font, brush, txtRF,flags);//画字符串
g.DrawRectangle(Pens.Black, Rectangle.Round(txtRF));//画框
}public void AnimateImage()
{
if (!currentlyAnimating)
{
//Begin the animation only once.
ImageAnimator.Animate(image, new EventHandler(this.OnFrameChanged));
currentlyAnimating = true;
}
} /// <summary>
/// 动画有关
/// </summary>
private void OnFrameChanged(object o, EventArgs e)
{
//Force a call to the Paint event handler.
//在类里没有发现使自己的实例无效重画的方法,所以动画功能没有实现
}
private Rectangle iconRect;//图标矩形(不包括文本)public DrawIcon(Bitmap iconImage, string iconText, int x, int y)//构造函数
{
image = iconImage;
Text = iconText;
SetIcon(x, y, 16, 16);
}/// <summary>
/// 设置图标的大小,位置等
/// </summary>
protected void SetIcon(int x, int y, int width, int height)
{
iconRect.X = x;
iconRect.Y = y;
iconRect.Width = width;
iconRect.Height = height;
}
再实现GraphicsList类,把所有生成的DrawObject对象添加到这个集合里,以后循环这个集合里的Draw方法就可以实现对所有画板上的对象重绘了.最后实现出来的效果就是一个动画(初始16x16大小),动画下面是一行文本,能放大缩小,移动.不过目前动画还没能实现.....不知道我贴出的代码能否说明问题,如果还有什么需要请回贴.