高速绘制图形 [英] Drawing Graphics at high speed

查看:90
本文介绍了高速绘制图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个项目,我试图渲染一些简单的图形并以相当高的速度刷新它们.我有一些工作要做,但是我在质疑是否有更好,更快/更少的处理器密集型解决方案.

我有一个1000个元素的整数数组,用作移位寄存器.阵列代表了产品在生产线上向下移动时好坏产品的位置. "0"是好产品,"1"是坏产品.产品以50mS的间隔沿生产线下移.因此,阵列位置每50 mS向右移动一个位置.

我想做的是通过在面板上创建图形来对此数组进行漂亮的可视化表示.为此,我创建了一个矩形数组,遍历我的整数数组,并使用画笔根据相应的整数数组的值填充矩形的颜色.然后使用图形更新面板.整个过程每隔50mS刷新一次.

欢迎对我的方法提出任何意见并提出更好的方法.

这是一些代码:

Hello,

I''ve got a project where I''m trying to render some simple graphics and refresh them at a fairly high speed. I''ve got something working, but I''m questioning if there''s a better / faster / less processor intensive appraoch.

I have an integer array of 1000 elements that acts as a shift register. The array represents positions of good and bad product as it travels down a production line. "0" being a good product, "1" being a bad product. Products travels down the line at 50mS intervals. Thus the array positions are being shifted to the right by one position every 50 mS.

What I''m trying to do is make a nice visual representation of this this array by creating graphics on a panel. To do this I''m creating an array of rectangles, looping through my integer array, and using brushes to fill the rectangles color based the corresponding integer array''s value. Then update the panel with the graphics. And the whole thing refreshes every 50mS.

Any opinions on my approach and suggestions for a better approach are welcome.

Here''s some code:

public partial class LaneControl : UserControl
    {
        Rectangle[] label = new Rectangle[1000];
        int[] status = new int[1000];

        public LaneControl()
        {
            InitializeComponent();
            Draw_Lane();
        }

        public void Draw_Lane()
        {

            int xOffset = 6;

            for (int i = 0; i < label.Length; i++)
            {
                label[i].X = 0;
                label[i].Y = 4;
                label[i].Height = 16;
                label[i].Width = 4;
                label[i].Offset(i * xOffset, 0);
            }

            panel1.Invalidate();
        }

        public void SetLabelStatus(int index)
        {
            status[index] = 1;

            panel1.Invalidate();
        }

        public void Shift()
        {
            Array.Copy(status, 0, status, 1, status.Length - 1);
            status[0] = 0;

            panel1.Invalidate();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics lane1 = panel1.CreateGraphics();

            for (int i = 0; i < label.Length; i++)
            {
                if (status[i] == 1)
                {
                    lane1.FillRectangle(Brushes.Red, label[i]);
                }
                else
                {
                    lane1.FillRectangle(Brushes.White, label[i]);
                }

                lane1.DrawRectangle(new Pen(Color.Black), label[i]);
            }
        }
    }

推荐答案

重要评论:您有责任自己清理一下!您不能创建Graphics对象,除非您也将其处置-它们是非常稀缺的资源,并且在Garbage Collector释放它们之前很久就会用完.优先使用using块,因为这将为您完成工作:
Important comment: You are responsible for clearing up after yourself! You cannot create Graphics objects unless you also Dispose them - they are a very scarce resource and will run out long before the Garbage Collector gets around to freeing them up. By preference, use a using block, as this will do the job for you:
using (Graphics lane1 = panel1.CreateGraphics())
   {
   // Use lane1 for drawing.
   }

笔,刷子等也是如此.
但是,您甚至根本不需要创建图形对象!任何控件的Paint事件都会为您提供量身定制的图形上下文,并在其后面进行清理:

The same also applies to Pens, Brushes, and so forth.
But you don''t even need to create a graphics object at all! The Paint event for any control hands you a tailor made graphics context, and cleans up behind itself:

private void panel1_Paint(object sender, PaintEventArgs e)
    {
    Graphics lane1 = e.Graphics;

    for (int i = 0; i < label.Length; i++)
        {
        if (status[i] == 1)
            {
            lane1.FillRectangle(Brushes.Red, label[i]);
            }
        else
            {
            lane1.FillRectangle(Brushes.White, label[i]);
            }
        lane1.DrawRectangle(new Pen(Color.Black), label[i]);
        }
    }

要稍微加快速度,请不要在循环中创建新的画笔-实际上,不要在Paint例程中创建它,而是将其创建为类级别变量,并使用它:

To speed this up a little bit, do not create a new brush in the loop - in fact do not create it in the Paint routine, create it as a class level variable, and use that:

private Pen penBlack = new Pen(Color.Black);
private void panel1_Paint(object sender, PaintEventArgs e)
    {
    Graphics lane1 = e.Graphics;

    for (int i = 0; i < label.Length; i++)
        {
        if (status[i] == 1)
            {
            lane1.FillRectangle(Brushes.Red, label[i]);
            }
        else
            {
            lane1.FillRectangle(Brushes.White, label[i]);
            }
        lane1.DrawRectangle(penBlack, label[i]);
        }
    }


这篇关于高速绘制图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆