GDI+绘图时如何减少cpu占用量? 如题,在picturebox的Paint事件中绘图,明明这图已经画好了,为什么CPU还在50%这么高(难不成一直在运行Paint事件?)内存还行吧,顺便也讲讲怎么减少内存占用量,本人刚接触GDI+,谢谢啦 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 绘的是啥图?如果直接draw一个现成的图,这个图的分辨率和色深除非非常BT的高,不然不会你这个样子,还有就是你自己写的函数,这个图比较复杂也会这样子。首先检查是否释放资源,然后看看算法本身是否有问题,在一个就是事件处理部分有没有问题。 private float font_size = 12;//显示字体大小 建议值:8~15 private int child_rect_width = 180;//建议值:150~200 private int draw_model = 0; private Bitmap bitmap; //测试数据 string father_name = "[email protected]"; string[] child_name ={ "[email protected]", "[email protected]", "[email protected]"} bool[] isreceived = { true, true, false} private void relation_Paint(object sender, PaintEventArgs e) { try { bitmap = new Bitmap(this.Size.Width, this.Size.Height); Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.AntiAlias;//消除锯齿 Font font = new Font("Times New Roman", font_size, FontStyle.Regular);//创建字体 SizeF fsf = g.MeasureString(father_name, font); //测试父节点文字所占像素大小 StringFormat drawFormat = new StringFormat(); drawFormat.FormatFlags = StringFormatFlags.DirectionVertical; Point father_center_point = new Point((int)(0.5 * this.Width), (int)(0.5 * this.Height));//panel_pic_relation中心作为父节点中心坐标 Point father_start_point = new Point(); //创建父节点起始坐标 father_start_point.X = (int)(father_center_point.X - 0.5 * fsf.Height); father_start_point.Y = (int)(father_center_point.Y - 0.5 * fsf.Width); g.DrawString(father_name, font, Brushes.Red, father_start_point, drawFormat);//画文字 g.DrawRectangle(new Pen(Color.Red), new Rectangle(father_start_point, new Size((int)fsf.Height, (int)fsf.Width)));//画矩形 ...//子节点的一些点计算并画图 字数限制贴不下 this.Image = bitmap; g.Dispose(); GC.Collect(); } catch { } }代码比较长啊 主要是一些点的计算 但也不应该占这么大CPU啊我另外做了计算量小的类似GDI绘图程序,也是50%的CPU占用量,代码如下 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private System.Drawing.Bitmap btp; private void pictureBox1_Paint(object sender, PaintEventArgs e) { btp = new Bitmap(300, 300); Graphics graphics = Graphics.FromImage(btp); Rectangle rectangle = new Rectangle(10, 10, 200, 200); graphics.DrawEllipse(Pens.Black, rectangle); graphics.DrawRectangle(Pens.Red, rectangle); pictureBox1.Image = btp; graphics.Dispose(); btp.Dispose(); //pictureBox1.Image.Save("c:\\1.bmp"); } } Graphics graphics = Graphics.FromImage(btp);=====>>>Graphics graphics = e.Graphics; 这样画确实CPU占用率几乎为0了 但是我还要保存画的图就保存不了了 Image img=pictureBox1.Image.Clone();img.Save(.....); btp = new Bitmap(300, 300);pictureBox1.Image = btp;btp.Dispose();上面这些都可以去掉了。 断点发现img 为 null ,网上查了下,发现用e.Graphics;这种方法对Image属性没影响的。。 Paint事 时时刻刻都在运行 我发现了 Paint事件里只能用e.Graphics 来写,用Graphics.FromImage(btp); 会不断地运行Paint事件用pictureBox1.CreateGraphics 画的图一闪就没了,不知道为什么而e.Graphics 来写 每切换回来一次,运行一次Paint事件 这是我所需要的就是不知道用e.Graphics 来写怎么把图保存下来,我是用SaveFileDialog来保存,发现pictureBox1.Image一直是null 继续求解 急需软件群发例子 datagridview不能显示相应数据 mysql c# MySql.Data.dll库存储图片问题 为什么会卡死呢 关于自定义控件(新手问题) 如何在web頁(Default.aspx)的textbox上增加鼠標的單擊事件 如何设置label[i]的值?i是程序中生成的 给IIS建个虚拟目录出错???怎么才对呀 C#如何上传文件 C#怎么根据Word模板生成一份Word文档 comboBox 数据捆绑问题 ■64位Win2003机器上,WinForm上使用ocx组件问题■
绘的是啥图?如果直接draw一个现成的图,这个图的分辨率和色深除非非常BT的高,不然不会你这个样子,还有就是你自己写的函数,这个图比较复杂也会这样子。首先检查是否释放资源,然后看看算法本身是否有问题,在一个就是事件处理部分有没有问题。
private float font_size = 12;//显示字体大小 建议值:8~15
private int child_rect_width = 180;//建议值:150~200
private int draw_model = 0;
private Bitmap bitmap;
//测试数据
string father_name = "[email protected]";
string[] child_name ={ "[email protected]", "[email protected]", "[email protected]"}
bool[] isreceived = { true, true, false} private void relation_Paint(object sender, PaintEventArgs e)
{
try
{
bitmap = new Bitmap(this.Size.Width, this.Size.Height); Graphics g = Graphics.FromImage(bitmap);
g.SmoothingMode = SmoothingMode.AntiAlias;//消除锯齿
Font font = new Font("Times New Roman", font_size, FontStyle.Regular);//创建字体
SizeF fsf = g.MeasureString(father_name, font); //测试父节点文字所占像素大小
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionVertical; Point father_center_point = new Point((int)(0.5 * this.Width), (int)(0.5 * this.Height));//panel_pic_relation中心作为父节点中心坐标
Point father_start_point = new Point(); //创建父节点起始坐标
father_start_point.X = (int)(father_center_point.X - 0.5 * fsf.Height);
father_start_point.Y = (int)(father_center_point.Y - 0.5 * fsf.Width); g.DrawString(father_name, font, Brushes.Red, father_start_point, drawFormat);//画文字
g.DrawRectangle(new Pen(Color.Red), new Rectangle(father_start_point, new Size((int)fsf.Height, (int)fsf.Width)));//画矩形
...//子节点的一些点计算并画图 字数限制贴不下 this.Image = bitmap;
g.Dispose();
GC.Collect();
}
catch { }
}代码比较长啊 主要是一些点的计算 但也不应该占这么大CPU啊我另外做了计算量小的类似GDI绘图程序,也是50%的CPU占用量,代码如下
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private System.Drawing.Bitmap btp;
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
btp = new Bitmap(300, 300);
Graphics graphics = Graphics.FromImage(btp);
Rectangle rectangle = new Rectangle(10, 10, 200, 200);
graphics.DrawEllipse(Pens.Black, rectangle);
graphics.DrawRectangle(Pens.Red, rectangle); pictureBox1.Image = btp;
graphics.Dispose();
btp.Dispose();
//pictureBox1.Image.Save("c:\\1.bmp");
}
}
Image img=pictureBox1.Image.Clone();
img.Save(.....);
pictureBox1.Image = btp;
btp.Dispose();
上面这些都可以去掉了。