该守则如下。 它接收来自Form1的列表中的文件路径,然后整个转换发生在这里,一切正常,但问题是progressbar1没有进展..可能是什么原因? 提前致谢!

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        numericUpDown1.Enabled = false;
        numericUpDown2.Enabled = false;
        button1.Enabled = false;
    }

    List<string> filepath_ = new List<string>();
    int count = 0, total = 0;
    private string format = string.Empty;
    private int _height = 0;
    private int _width = 0;

    internal void passpath(List<string> fp)
    {
        filepath_.AddRange(fp);
        total = filepath_.Count;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (button1.Text == "Stop")
        {
            backgroundWorker1.CancelAsync();
            button1.Text = "Transform";
            return;
        }
        else
        {
            button1.Text = "Stop";
            System.Threading.Thread.Sleep(1);
            backgroundWorker1.RunWorkerAsync();
        }
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        if (backgroundWorker1.CancellationPending)
        {
            e.Cancel = true;
            return;
        }

        foreach (string filepath in filepath_)
        {
            if (backgroundWorker1.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            ImageFormat imageFormat = ImageFormat.Jpeg;
            switch (format)
            {
                case "JPEG":
                    imageFormat = ImageFormat.Jpeg;
                    break;
                    break;
                default:
                    break;
            }
            string finalpath = filepath.Substring(0, filepath.IndexOf('.'));
            Image image1 = Image.FromFile(filepath);
            string ext = Path.GetExtension(filepath);

            if (_height != 0 && _width != 0 && format!=string.Empty)
            {
                Bitmap bitmap = new Bitmap(image1, _width, _height);
                bitmap.Save(finalpath + " (" + _width.ToString() + "x" + _height.ToString() + ")." +format, imageFormat);
            }
            else if (_height != 0 && _width != 0)
            {
                Bitmap bitmap = new Bitmap(image1, _width, _height);
                bitmap.Save(finalpath + " (" + _width.ToString() + "x" + _height.ToString() + ")" + ext);
            }
            else if (format != string.Empty)
            {
                Bitmap bitmap = new Bitmap(image1);
                bitmap.Save(finalpath+"." + format, imageFormat);
            }
            count++;

            int i = ((count / total) * 100);

            backgroundWorker1.ReportProgress(i);
            System.Threading.Thread.Sleep(1);
        }
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;

        label6.Text = count.ToString() + " Out of " + total.ToString() + " Images transformed";


    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        button1.Text = "Transform";
        progressBar1.Value = 0;

        if (e.Cancelled)
            MessageBox.Show("Transformation stopped. " + count.ToString() + " images transformed.");
        else if (e.Error != null)
            MessageBox.Show(e.Error.Message);
        else
        {
            MessageBox.Show(count.ToString() + " Images Transformed");
            Application.Exit();
        }
    }

    private void listBox1_SelectedValueChanged(object sender, EventArgs e)
    {
        format = ((ListBox)sender).SelectedItem.ToString();
        button1.Enabled = true;
    }

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        _width = 640;
        _height = 480;
        button1.Enabled = true;
    }
    }
}
}

===============>>#1 票数:1

将变量更改为双精度数:

double count = 0, total = 0;

当你做这个计算时:

int i = ((count / total) * 100);

你目前正在进行整数运算。 count / total (即1 / 104 / 10 )的结果四舍五入为0 当除以100 ,结果仍为0 ,因此ProgressBar不会移动。

使用double类型将正确存储商的小数部分。

===============>>#2 票数:1

Grant Winney已经指出了这个问题:它是整数除法。

在不更改count和/或total数据类型的情况下,您可以使用:

int i = (int)((100.0 * count) / total)

或者那个:

int i = (100 * count) / total

前者使得100.0 * count为双倍,而除以total 后者通过简单地首先乘以count (改变操作序列)来坚持整数运算,这样只要count不为0,除法就不会为0。

===============>>#3 票数:0

pBar.Step = 2;
pBar.PerformStep();

  ask by translate from so

未解决问题?本站智能推荐: