如何在Delphi中添加越来越多的已保存文件?
我正在制作一个程序,它可以拍摄屏幕快照,并将其作为位图图片保存到文件夹中。 不过,我似乎遇到了问题,图片只是覆盖了自己。
谁能告诉我如何才能使保存时的数字比上一个大1? 例如: 保存 1:Screenshot0001.bmp 保存 2:Screenshot0002.bmp 保存3:Screenshot0003.bmp
等。
I'm making a program that takes a snapshot of the screen, and saves it to a folder as a Bitmap picture. I seem to be running into a problem though, the picture just overwrites itself.
Can anyone tell me how I can make it so when it saves, the number will be one higher than the last? For example: Save 1: Screenshot0001.bmp Save 2: Screenshot0002.bmp
Save 3: Screenshot0003.bmp
And so on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有很多方法可以完成这样的事情。
像数码相机一样操作; 拥有一个计数器并将其保存在文件或注册表中。 您可能会遇到多用户问题,并且仍然必须处理图像已存在的情况。
不要使用递增的数字,而是在文件名中写入日期时间。
FileName := 'Screenshot_'+FormatDateTime('yyyymmdd-hhnnss-zzz.bmp',now());
执行类似于下面的代码的操作来查找最新的号码。 我认为这符合您所描述的目的,但请记住,随着您编写更多图像,此代码会变得更慢。 如果有数千张图片和缓慢的驱动器或网络,它可能会“挂起”您的程序。
..
There are many ways to accomplish something like this.
Do it like your digital camera does; Have a counter and save it in a file or in the registry. You'll probably run into multi-user problems and you'll still have to handle situations where an image already exists.
Don't use a incrementing number, but write a datetime in the filename.
FileName := 'Screenshot_'+FormatDateTime('yyyymmdd-hhnnss-zzz.bmp',now());
Do something like the code below to find the latest number. I think this does what you're describing, but remember that this code will get slower as you write more images. With thousands of pictures and a slow drive or network it could 'hang' your program.
..
在程序启动时,迭代所有 Screenshot*.bmp 文件,解析出数字部分并找到最高的 - 将此值分配给您的计数器。 执行快照时,进入一个循环,尝试使用“仅在不存在时才创建”(CREATE_NEW) 语义创建 Screenshot.bmp,递增计数器,直到找到未使用的名称。
或者,使用时间戳而不是计数器:)
At program startup, iterate all Screenshot*.bmp files, parse out the numeric part and find the highest - assign this value to your counter. When doing a snapshot, go into a loop that tries crreating Screenshot.bmp with "only create if doesn't exist already" (CREATE_NEW) semantics, incrementing the counter until you find an unused name.
Alternately, use timestamp instead of counter :)
您需要一个像这样的例程,它模仿 Windows 文件复制,其中第一个文件是“我的文件”,第二个是“我的文件(2)”,然后是“我的文件(3)”等。
You need a routine like this which mimics Windows file duplication where the first file is 'My File', the second is 'My File (2)', then 'My File (3)' etc.