C#中for循环内height值未随index更新问题求助
问题:循环中height值始终固定为180,无法随index变化更新
我需要在每个循环周期中通过公式计算新的height整数值,但尽管index变量在循环中不断递增,height始终等于180。相关代码及控制台输出如下:
internal class Program { static void Main(string[] args) { ///some code WorkContainer.StartUp(Source, Mode, Output); } } internal class WorkContainer { public static void StartUp(string Source, string Mode, string Output) { ///some code int maxWidth = 180; int maxHeight = 420; int framerate = 30; switch (Mode) { case "Bouncing": int delta = 5; double bouncesPerSecond = 1.5; int index = 0; int height = (int)(index == 0 ? maxHeight : (Math.Floor(Math.Abs(Math.Cos(index / (framerate / bouncesPerSecond) * Math.PI) * (maxHeight - delta))) + delta)); ffmpegWork.ffmpegPngChangeResolution(CurrentDirectory, RandomTempFolderIdentity, index, maxWidth, height); break; } } } internal class ffmpegWork { public static void ffmpegPngChangeResolution(string CurrentDirectory, string RandomTempFolderIdentity, int index, int maxWidth, int height) { int ImgValues = new DirectoryInfo($"{CurrentDirectory}/Temp{RandomTempFolderIdentity}/img/").GetFiles().Length; for (int i = 0; i < ImgValues; i++, index++) { try { Process compiler = new Process(); compiler.StartInfo.FileName = $"{CurrentDirectory}/ffmpeg/bin/ffmpeg"; compiler.StartInfo.Arguments = $"-y -i {CurrentDirectory}/Temp{RandomTempFolderIdentity}/img/img-{i + 1}.png -vf scale={maxWidth}:{height} -c:a libvorbis {CurrentDirectory}/Temp{RandomTempFolderIdentity}/webmImg/img-{i + 1}.png"; compiler.StartInfo.RedirectStandardOutput = true; compiler.StartInfo.CreateNoWindow = true; compiler.StartInfo.UseShellExecute = false; compiler.Start(); Console.WriteLine($"png frame - {i + 1}/{ImgValues}\nheight={height}"); } catch (Exception e) { Console.WriteLine(e.Message); } } } }
控制台输出
png frame - 1/n height=180 png frame - 2/n height=180 png frame - 3/n height=180
问题原因
- height值仅初始化一次:在
WorkContainer.StartUp方法中,height只在调用ffmpegPngChangeResolution前计算了一次,之后传入方法的是固定值,循环内部没有重新计算。 - 整数除法潜在风险:虽然当前代码中
framerate / bouncesPerSecond会得到浮点值,但如果后续参数类型调整,index(整数)参与除法可能导致精度丢失,影响余弦函数的计算结果。
修复方案
将height的计算逻辑移到循环内部,同时把计算所需的参数传入方法,确保每次循环都能根据当前index重新计算:
1. 修改WorkContainer.StartUp中的调用逻辑
case "Bouncing": int delta = 5; double bouncesPerSecond = 1.5; int index = 0; // 不再提前计算height,传入计算所需的所有参数 ffmpegWork.ffmpegPngChangeResolution(CurrentDirectory, RandomTempFolderIdentity, index, maxWidth, maxHeight, delta, framerate, bouncesPerSecond); break;
2. 修改ffmpegPngChangeResolution方法
internal class ffmpegWork { public static void ffmpegPngChangeResolution(string CurrentDirectory, string RandomTempFolderIdentity, int index, int maxWidth, int maxHeight, int delta, int framerate, double bouncesPerSecond) { int ImgValues = new DirectoryInfo($"{CurrentDirectory}/Temp{RandomTempFolderIdentity}/img/").GetFiles().Length; for (int i = 0; i < ImgValues; i++, index++) { try { // 每次循环重新计算height int height = (int)(index == 0 ? maxHeight : (Math.Floor(Math.Abs(Math.Cos((double)index / (framerate / bouncesPerSecond) * Math.PI) * (maxHeight - delta))) + delta)); Process compiler = new Process(); compiler.StartInfo.FileName = $"{CurrentDirectory}/ffmpeg/bin/ffmpeg"; compiler.StartInfo.Arguments = $"-y -i {CurrentDirectory}/Temp{RandomTempFolderIdentity}/img/img-{i + 1}.png -vf scale={maxWidth}:{height} -c:a libvorbis {CurrentDirectory}/Temp{RandomTempFolderIdentity}/webmImg/img-{i + 1}.png"; compiler.StartInfo.RedirectStandardOutput = true; compiler.StartInfo.CreateNoWindow = true; compiler.StartInfo.UseShellExecute = false; compiler.Start(); Console.WriteLine($"png frame - {i + 1}/{ImgValues}\nheight={height}"); } catch (Exception e) { Console.WriteLine(e.Message); } } } }
关键调整说明
- 将height计算逻辑移入循环,确保每次循环都能基于最新的index值重新计算。
- 显式将
index转换为double,避免整数除法可能导致的精度丢失问题,保证余弦函数计算的准确性。 - 传递计算height所需的全部参数,确保方法内的计算逻辑独立完整。
内容的提问来源于stack exchange,提问作者Dokoa
相关产品推荐
相关产品推荐

