2

我有一个用户控件,我在控件构造函数中创建了一些钢笔和画笔,并使用它们在位图​​上绘图,我正在使用.Net Compact Framework,Graphics如果我有三个,我发现它的行为非常奇怪,Pens假设Green, Red and Blue我更新我的位图每秒钟都会100 ms创建20笔,这意味着如果我有三个相同颜色的笔,那么它只会创建.2 Pens100 msPens1 Pen

示例代码:

       public MyTestControl()
        {
            InitializeComponent();
            _barBrush = new SolidBrush(Color.Red);
            _func1Pen = new Pen(Color.Red);
            _func2Pen = new Pen(Color.Blue);
            _func3Pen = new Pen(Color.Green);
        } 

      protected override void OnPaint(PaintEventArgs e)
        {
            if (_bmpScreen == null)
            {
                _bmpScreen = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
                _gBmp = Graphics.FromImage(_bmpScreen);    
            }

            _gBmp.Clear(Color.White);    
            DoDrawings();    
            //
            e.Graphics.DrawImage(_bmpScreen, 0, 0);
        }

     private void DoDrawings()
        {
            if (Data == null) return;

            switch (Data.Length)
            {
                case 1:
                    DrawGraph(Data[0].Where(val => val != Config.NO_DATA_VALUE).ToArray(), _func1Pen);
                    break;
                case 2:
                    DrawGraph(Data[0].Where(val => val != Config.NO_DATA_VALUE).ToArray(), _func1Pen);
                    DrawGraph(Data[1].Where(val => val != Config.NO_DATA_VALUE).ToArray(), _func2Pen);
                    break;
                case 3:

                    DrawGraph(Data[2].Where(val => val != Config.NO_DATA_VALUE).ToArray(), _func1Pen);
                    DrawGraph(Data[0].Where(val => val != Config.NO_DATA_VALUE).ToArray(), _func2Pen);
                    DrawGraph(Data[1].Where(val => val != Config.NO_DATA_VALUE).ToArray(), _func3Pen);
                    break;
                default:
                    break;
            }

        }

private void DrawGraph(int[] data, Pen pen)
        {
            if (data == null) return;           

            Point[] newPoints = GetDataPoints(data);
            DrawLines(newPoints, pen, _gBmp);           
        }



private void DrawLines(Point[] newPoints, Pen functionPen, Graphics g)
        {
            g.DrawLines(functionPen, newPoints);
            }

注意:我正在使用.Net Compact Framework 3.5并且我的刷新率是100 ms 我正在使用.Net Compact Framework Remote Performance Monitor的,它告诉我20 Pens每个second, Number of Pens Created increases by 20 every second.

知道2 Pens每次更新位图时会导致创建什么吗?

4

0 回答 0