提高绘图速度,使用双缓冲,告别BitBlt,以免你的内存被偷走

由于内存释放问题,使用BitBlt方法会导致内存占用慢慢长大而不会被释放,使用普通的双缓冲没有这个问题,而且速度可以
由于是裁剪了部分代码,因此存在代码不完整的问题
Cementing.CurveSplit.DrawCurvePress  再memDC指向的位图(memBmp)上画压力曲线图,其它类似
private Graphics memDC;      //屏幕外的图像
private Graphics clientDC;
private Bitmap memBmp;
private static SolidBrush backBrush;

#region DrawCurve BitBlt
private void DrawCurveBitBlt(int p_ScrollValue)
{
    try
    {
        if (m_RealTime == true)
        {
            mAL = Cementing.PortValues.AL;  //曲线图数据来源,mAL是System.Collections.ArrayList mAL
        }
        if (memBmp == null)                                         //初始化要双缓冲的位图
        {
            memBmp = null;
            memBmp = new Bitmap(m_intWidth, m_intHeight);
        }
        else
        {
            if ((mWidthOld != m_intWidth) || (mHeightOld != m_intHeight))   //位图尺寸发生了变化
            {
                memBmp = null;
                mWidthOld = m_intWidth;
                mHeightOld = m_intHeight;
                memBmp = new Bitmap(m_intWidth, m_intHeight);
            }
        }
        clientDC = this.CreateGraphics();   //获取绘图区的Graphics
        IntPtr hdc = clientDC.GetHdc();
        IntPtr memdc = Win32Support.CreateCompatibleDC(hdc);
        Win32Support.SelectObject(memdc, memBmp.GetHbitmap());
        memDC = Graphics.FromHdc(memdc);

        double pWidth = (double)(m_intWidth - m_H) / (double)m_WidthRange;
        double pHeight = (double)(m_intHeight - m_V) / (double)m_HeightRange;
        memDC.FillRectangle(Brushes.White, 0, 0, m_intWidth, m_intHeight);
        switch (m_intType)
        {
            case 1:
                SetScrollBar();
                Cementing.CurveSplit.DrawCurvePress(memDC, p_ScrollValue, mAL, mPen, backBrush, m_intWidth, m_intHeight);
                break;
            case 2:
                SetScrollBar();
                Cementing.CurveSplit.DrawCurvePressDif(memDC, p_ScrollValue, mAL, mPen, backBrush, m_intWidth, m_intHeight);
                break;
            case 3:
                SetScrollBar();
                Cementing.CurveSplit.DrawCurveRate(memDC, p_ScrollValue, mAL, mPen, backBrush, m_intWidth, m_intHeight);
                break;
            case 4:
                SetScrollBar();
                Cementing.CurveSplit.DrawCurveTemperature(memDC, p_ScrollValue, mAL, mPen, backBrush, m_intWidth, m_intHeight);
                break;
        }
        IntPtr hMemdc = memDC.GetHdc();
        Win32Support.BitBlt(hdc, 0, 0, m_intWidth, m_intHeight, hMemdc, 0, 0, Win32Support.TernaryRasterOperations.SRCCOPY);
        clientDC.ReleaseHdc(hdc);
        memDC.ReleaseHdc(hMemdc);
    }
    catch (NullReferenceException NullEx)
    {
        throw NullEx;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
#endregion DrawCurve BitBlt
#region DrawCurve
private void DrawCurve(int p_ScrollValue)
{
    try
    {
        if (m_RealTime == true)
        {
            mAL = Cementing.PortValues.AL;  //曲线图数据来源,mAL是System.Collections.ArrayList mAL
        }
        if (memBmp == null)                                         //初始化要双缓冲的位图
        {
            memBmp = null;
            memBmp = new Bitmap(m_intWidth, m_intHeight);
        }
        else
        {
            if ((mWidthOld != m_intWidth) || (mHeightOld != m_intHeight))
            {
                memBmp = null;
                mWidthOld = m_intWidth;
                mHeightOld = m_intHeight;
                memBmp = new Bitmap(m_intWidth, m_intHeight);
            }
        }
        memDC = Graphics.FromImage(memBmp);
        memDC.Clear(Color.White);
        backBrush = new SolidBrush(Color.White);
        memDC.FillRectangle(backBrush, 0, 0, m_intWidth, m_intHeight);
        switch (m_intType)
        {
            case 1:
                SetScrollBar();
                Cementing.CurveSplit.DrawCurvePress(memDC, p_ScrollValue, mAL, mPen, backBrush, m_intWidth, m_intHeight);
                break;
            case 2:
                SetScrollBar();
                Cementing.CurveSplit.DrawCurvePressDif(memDC, p_ScrollValue, mAL, mPen, backBrush, m_intWidth, m_intHeight);
                break;
            case 3:
                SetScrollBar();
                Cementing.CurveSplit.DrawCurveRate(memDC, p_ScrollValue, mAL, mPen, backBrush, m_RealTime, m_intWidth, m_intHeight);
                break;
            case 4:
                SetScrollBar();
                Cementing.CurveSplit.DrawCurveTemperature(memDC, p_ScrollValue, mAL, mPen, backBrush, m_intWidth, m_intHeight);
                break;
        }
        clientDC = this.CreateGraphics();
        clientDC.DrawImage(memBmp, 0, 0);
    }
    catch (NullReferenceException NullEx)
    {
        throw NullEx;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
#endregion DrawCurve
 
 
 

你可能感兴趣的:(职场,内存,休闲,双缓冲)