行:如何解決異常當字符串是「9f」時,輸入字符串的格式不正確?
currentFrameInt = System.Convert.ToInt32(currentFramestr);
在這種情況下,我在currentFramestr看到值「9F」 包括字符串的開頭有三個空格。
的currentFramestr是更新progressBar1的一部分:
if (strFFOUT.Contains("frame="))
{
currentFramestr = strFFOUT.Substring(7, 6);
currentFramestr = currentFramestr.Trim();
currentFrameInt = System.Convert.ToInt32(currentFramestr, 16);
}
string percentage = System.Convert.ToInt32((ProgressBar1.Value/ProgressBar1.Maximum * 100)).ToString() + "%";
ProgressBar1.Maximum = FCount + 1000;
ProgressBar1.Value = (currentFrameInt);
什麼我想要做的是更新進度條值。
也許整個方法代碼會給我更多的線索,我想要什麼和使用currentFramestr。一般來說,我想更新progressBar1值。
private void Convert()
{
Control.CheckForIllegalCrossThreadCalls = false;
if (ComboBox1.SelectedIndex == 3)
{
strFFCMD = " -i \"" + InputFile + "\" \"" + OutputFile + "\"";
}
if (ComboBox1.SelectedIndex == 2)
{
strFFCMD = " -i " + (char)34 + InputFile + (char)34 +
" -c:v libx264 -s 1280x720 -pix_fmt yuv420p -qp 20 -profile high444-c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 " + OutputFile;
}
psiProcInfo.FileName = exepath;
psiProcInfo.Arguments = strFFCMD;
psiProcInfo.UseShellExecute = false;
psiProcInfo.WindowStyle = ProcessWindowStyle.Hidden;
psiProcInfo.RedirectStandardError = true;
psiProcInfo.RedirectStandardOutput = true;
psiProcInfo.CreateNoWindow = true;
prcFFMPEG.StartInfo = psiProcInfo;
prcFFMPEG.Start();
ffReader = prcFFMPEG.StandardError;
do
{
if (Bgw1.CancellationPending)
{
return;
}
Button5.Enabled = true;
Button3.Enabled = false;
strFFOUT = ffReader.ReadLine();
RichTextBox1.Text = strFFOUT;
if (strFFOUT.Contains("frame="))
{
currentFramestr = strFFOUT.Substring(7, 6);
currentFramestr = currentFramestr.Trim();
currentFrameInt = System.Convert.ToInt32(currentFramestr, 16);
}
string percentage = System.Convert.ToInt32((ProgressBar1.Value/ProgressBar1.Maximum * 100)).ToString() + "%";
ProgressBar1.Maximum = FCount + 1000;
ProgressBar1.Value = (currentFrameInt);
Label12.Text = "Current Encoded Frame: " + currentFrameInt;
Label11.Text = percentage;
} while (!(prcFFMPEG.HasExited || string.IsNullOrEmpty(strFFOUT)));
}
您可以使用tryparse或捕獲錯誤並告訴用戶輸入垃圾?取決於你的應用程序最適合你的效果。 – BugFinder
9f不是Convert.ToInt32可以處理的東西。不是一個整數(除非是基數16)。您是否對9(或可以轉換爲整數的任何初始字符)感興趣?或者您想僅告訴用戶輸入錯誤? – Steve
或者您是否期望它將該解釋爲十六進制?領先空間怎麼樣?基本上,你會期望*得到什麼樣的價值? –