C# - " .FillRectangle"不完全填充矩形

时间:2015-08-06 01:39:33

标签: c# graphics .net-3.5 picturebox visual-studio-2015

我使用以下代码作为我将来如何使用自定义进度条的测试 - 它实际上根本不是进度条,而是代码绘制的图片框矩形打开,然后根据计时器填写。

问题是,在填充框之前我达到了100%。我已经修好了,但找不到问题。我究竟做错了什么?请参阅下面的代码,并在我的系统上显示行为的屏幕截图。

感谢。

    public partial class frmLoading : Form
{
    System.Windows.Forms.Timer tLoading = new System.Windows.Forms.Timer();
    Double pbLoadingUnit;
    int pbLoadingWIDTH, pbLoadingHEIGHT, pbLoadingComplete;

    Bitmap bmpLoading;
    Graphics gLoading;

    private void frmLoading_Load(object sender, EventArgs e)
    {
        pbLoadingWIDTH = pictureLoading.Width;
        pbLoadingHEIGHT = pictureLoading.Height;
        pbLoadingUnit = pbLoadingWIDTH / 100;
        pbLoadingComplete = 0;
        bmpLoading = new Bitmap(pbLoadingWIDTH, pbLoadingHEIGHT);
        tLoading.Interval = 32;
        tLoading.Tick += new EventHandler(this.tLoading_Tick);
        tLoading.Start();
    }
    private void tLoading_Tick(object sender, EventArgs e)
    {
        gLoading = Graphics.FromImage(bmpLoading);
        gLoading.Clear(Color.DarkSlateGray);
        gLoading.FillRectangle(Brushes.DodgerBlue, new Rectangle(0, 0, (int)(pbLoadingComplete * pbLoadingUnit), pbLoadingHEIGHT));
        gLoading.DrawString(pbLoadingComplete + "%", new Font("Segoe UI Semibold", pbLoadingHEIGHT / 2), Brushes.White, new PointF(pbLoadingWIDTH / 2 - pbLoadingHEIGHT, pbLoadingHEIGHT / 10));
        pictureLoading.Image = bmpLoading;

        pbLoadingComplete++;
        if (pbLoadingComplete > 100)
        {
            gLoading.Dispose();
            tLoading.Stop();
        }
    }

3 个答案:

答案 0 :(得分:1)

你应该改变这个

pbLoadingUnit = pbLoadingWIDTH / 100;

到这个

pbLoadingUnit = Convert.ToDouble(pbLoadingWIDTH) / 100;

我认为你的图片宽度不是一百的倍数。 使用旧代码,您的pbLoadingUnit将生成为整数,因为您将整数除以整数。

您也可以使用它:

double div = 100;
pbLoadingUnit = pbLoadingWIDTH / div;

OR

pbLoadingUnit = pbLoadingWIDTH / Convert.ToDouble(100);

关键是,您应该获得double的{​​{1}}值。

有关数字投射的详细信息,您可能会看到此链接dotnetperls.com/numeric-casts

答案 1 :(得分:0)

尝试将以下行放在 /home/vagrant/portal-api/portal-api/node_modules/express/lib/router/index.js:458 throw new TypeError('Router.use() requires middleware function but got a ^ TypeError: Router.use() requires middleware function but got a Object at Function.use (/home/vagrant/portal-api/portal-api/node_modules/express/lib/router/index.js:458:13) at EventEmitter.<anonymous> (/home/vagrant/portal-api/portal-api/node_modules/express/lib/application.js:219:21) at Array.forEach (native) at EventEmitter.use (/home/vagrant/portal-api/portal-api/node_modules/express/lib/application.js:216:7) at Object.<anonymous> (/home/vagrant/portal-api/portal-api/app.js:31:5) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) 的{​​{1}}事件中:

OnResize()

您可能会在frmLoading期间获得pbLoadingWIDTH = pictureLoading.Width; pbLoadingHEIGHT = pictureLoading.Height; pbLoadingUnit = pbLoadingWIDTH / 100; 的初始尺寸...而不是在您的表单中显示并显示的实际pictureLoading

如果您已获得相应的Load() widthtloading.Start()对象,请size发生。

答案 2 :(得分:0)

你必须在int pbmodal中添加这个变量; 然后计算单位的MOD

表单加载

pbmodal = pbLoadingWIDTH%100;

当完成时100%添加模态。

分裂差异

`if(pbLoadingComplete&gt; 100)

{ gLoading.FillRectangle(Brushes.DodgerBlue,new Rectangle(0,0,(int)(pbLoadingComplete * pbLoadingUnit)+ pbmodal,pbLoadingHEIGHT));                 gLoading.DrawString(pbLoadingComplete-1 +“%”,new Font(“Segoe UI Semibold”,pbLoadingHEIGHT / 2),Brushes.White,new PointF(pbLoadingWIDTH / 2 - pbLoadingHEIGHT,pbLoadingHEIGHT / 10));                 pictureLoading.Image = bmpLoading;             }`