C#进度条坏渲染

时间:2015-08-20 21:35:50

标签: c# .net progress-bar

我用图片创建了自定义进度条,但我遇到了问题。从进度条的50%显示空格。

进度条在这里:http://i.stack.imgur.com/d686B.png 有控制库代码:

public partial class UserControl1: UserControl
{
    public UserControl1() : base()
    {
        InitializeComponent();
        SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
    }

    private void UserControl1_Load(object sender, EventArgs e)
    {

    }

    bool _revealImage = true;
    int _maximum = 100;
    int _value = 0;
    Image _progressbarImage = Properties.Resources.progress;
    Color _progressbarColor = Color.Transparent;

    protected override void OnResize(EventArgs e)
    {
        // Invalidate the control to get a repaint.
        this.Invalidate();
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {

        Graphics g = e.Graphics;
        Rectangle r = this.ClientRectangle;

        ControlPaint.DrawBorder(g, r, Color.Transparent, ButtonBorderStyle.Solid);
        float percent = (float)_value / (float)(_maximum);
        //r.Inflate(-1, -1);
        r.Width = (int)((float)r.Width * percent);
        if ((r.Width < 1))
        {
            return;
        }

        if (_progressbarImage == null)
        {
            using (SolidBrush b = new SolidBrush(_progressbarColor))
            {
                g.FillRectangle(b, r);
                b.Dispose();
            }
        }
        else
        {
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            if (_revealImage)
            {
                g.DrawImage(_progressbarImage, r, r, GraphicsUnit.Pixel);
            }
            else
            {
                g.DrawImage(_progressbarImage, r);
            }
        }
        g.Dispose();
    }

    public int Maximum
    {
        get
        {
            return _maximum;
        }
        set
        {
            _maximum = value;
            this.Invalidate();
        }
    }

    public int Value
    {
        get
        {
            return _value;
        }
        set
        {
           // _value = value;
            int oldValue = _value;
            // Make sure that the value does not stray outside the valid range.
            if (value < 0)
            {
                _value = 0;
            }
            else if (value > Maximum)
            {
                _value = Maximum;
            }
            else
            {
                _value = value;
            }

            // Invalidate only the changed area.
            float percent;

            Rectangle newValueRect = this.ClientRectangle;
            Rectangle oldValueRect = this.ClientRectangle;

            // Use a new value to calculate the rectangle for progress.
            percent = (float)(_value - 0) / (float)(Maximum - 0);
            newValueRect.Width = (int)((float)newValueRect.Width * percent);

            // Use an old value to calculate the rectangle for progress.
            percent = (float)(oldValue - 0) / (float)(Maximum - 0);
            oldValueRect.Width = (int)((float)oldValueRect.Width * percent);

            Rectangle updateRect = new Rectangle();

            // Find only the part of the screen that must be updated.
            if (newValueRect.Width > oldValueRect.Width)
            {
                updateRect.X = oldValueRect.Size.Width;
                updateRect.Width = newValueRect.Width - oldValueRect.Width;
            }
            else
            {
                updateRect.X = newValueRect.Size.Width;
                updateRect.Width = oldValueRect.Width - newValueRect.Width;
            }

            updateRect.Height = this.Height;

            // Invalidate the intersection region only.
            this.Invalidate(updateRect);
        }
    }

    public Image ProgressbarImage
    {
        get
        {
            return _progressbarImage;
        }
        set
        {
            _progressbarImage = value;
            this.Invalidate();
        }
    }

    public Color ProgressbarColor
    {
        get
        {
            return _progressbarColor;
        }
        set
        {
            _progressbarColor = value;
            this.Invalidate();
        }
    }

    public bool RevealImage
    {
        get
        {
            return _revealImage;
        }
        set
        {
            _revealImage = value;
            this.Invalidate();
        }
    }
}

感谢您的帮助。

0 个答案:

没有答案