2014-04-19 2 views
0

으로 진행되지 않습니다. 코드는 다음과 같습니다. 그것은 Form1에서 목록에있는 파일 경로를받습니다. 그리고 전체 변환이 여기에서 일어나고 모든 것이 잘 작동합니다.하지만 문제는 progressbar1이 진행되지 않는다는 것입니다. 이유가 무엇일까요? 미리 감사드립니다!진행률 표시 줄이 C#

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

아니므로 0이 아닐 것이라는 것을 문제와 관련이 없습니다. – JeffRSon

+0

정말 그 사실에 대해 정말 유감스럽게 생각합니다. 처음 엔 질문을 올렸습니다. 다음 시간은 신중합니다. –

답변

1

변경 복식에 당신 변수 :

double count = 0, total = 0; 

이 계산을 수행 할 때 : 당신은 정수 연산을하고있는

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

합니다. count/total (즉 1/10 또는 4/10)의 결과는 0으로 반올림됩니다. 이 값을 100으로 나눌 때 결과는 여전히 0이므로 ProgressBar은 이동하지 않습니다.

double 유형을 사용하면 몫의 소수 부분이 올바르게 저장됩니다.

+0

int i = (100 * count)/total; 더 이상 변경하지 않고 ... – JeffRSon

+0

@JeffRSon 좋은 대안 솔루션. 답변으로 게시해야합니다. –

+0

도움을 많이 주셔서 감사합니다. 1 가지 더 진행 표시 줄을 끝까지 끝내고 모든 이미지를 변환 한 후에도 계속 진행할 수 있습니까? –

1

이 문제는 이미 Grant Winney에 의해 지적되었습니다. 정수 구분입니다.

count의 데이터 유형을 변경하지 않고 및/또는 total이를 사용할 수 있습니다

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

또는 : 전자는 100.0 * count 더블과 분열을하게

int i = (100 * count)/total

total도 마찬가지입니다. 간단히, (연산의 변경 순서) 제 count 곱하여 정수 연산에 후자 스틱 등 분단 한 count 당신은 소스의 모든 부분을 삭제 한해야 0

0
pBar.Step = 2; 
pBar.PerformStep(); 
관련 문제