我正在制作带有pictureBox图片的trakbar。通过单击该轨迹栏,必须有一条黑色垂直线。但是下面有一个问题。
答案 0 :(得分:1)
你的问题是,将DateTime变成字符串并不会为你提供一个支持的文件名来保存图像。
例如:
String fileName = "C:\\" + DateTime.Now + ".bmp";
File.Create(fileName);
会抛出错误,因为fileName
会为您提供C:\08/07/2013 12:41:39.bmp
的路径 - 这不是有效的文件路径。
要解决此问题,您可以将字符串的DateTime部分格式化为更可口的部分,例如
String formattedDateTime = DateTime.Now.ToString("s").Replace(":","-");
String fileName = String.Format(@"C:\{0}.bmp", formattedDateTime);
File.Create(fileName);
这会给你一个像C:\2013-07-08T12-48-57.bmp
这样的文件名,它不仅可以保存,而且可以排序。
最后,要将其应用于您的代码,您将使用
String formattedDateTime = DateTime.Now.ToString("s").Replace(":","-") ;
String fileName = String.Format(@"C:\{0}.bmp", formattedDateTime);
img.Save(fileName);