如何在Delphi中添加越来越多的已保存文件?

发布于 2024-07-15 17:25:02 字数 194 浏览 5 评论 0原文

我正在制作一个程序,它可以拍摄屏幕快照,并将其作为位图图片保存到文件夹中。 不过,我似乎遇到了问题,图片只是覆盖了自己。

谁能告诉我如何才能使保存时的数字比上一个大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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泼猴你往哪里跑 2024-07-22 17:25:02

有很多方法可以完成这样的事情。

  1. 像数码相机一样操作; 拥有一个计数器并将其保存在文件或注册表中。 您可能会遇到多用户问题,并且仍然必须处理图像已存在的情况。

  2. 不要使用递增的数字,而是在文件名中写入日期时间。
    FileName := 'Screenshot_'+FormatDateTime('yyyymmdd-hhnnss-zzz.bmp',now());

  3. 执行类似于下面的代码的操作来查找最新的号码。 我认为这符合您所描述的目的,但请记住,随着您编写更多图像,此代码会变得更慢。 如果有数千张图片和缓慢的驱动器或网络,它可能会“挂起”您的程序。

..

i := 0;
while FileExists(Format('%sScreenshot%.04d.bmp',[ImgPath,i])) do
  inc(i);

There are many ways to accomplish something like this.

  1. 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.

  2. Don't use a incrementing number, but write a datetime in the filename.
    FileName := 'Screenshot_'+FormatDateTime('yyyymmdd-hhnnss-zzz.bmp',now());

  3. 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.

..

i := 0;
while FileExists(Format('%sScreenshot%.04d.bmp',[ImgPath,i])) do
  inc(i);
心清如水 2024-07-22 17:25:02

在程序启动时,迭代所有 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 :)

星星的轨迹 2024-07-22 17:25:02

您需要一个像这样的例程,它模仿 Windows 文件复制,其中第一个文件是“我的文件”,第二个是“我的文件(2)”,然后是“我的文件(3)”等。

function AppendDuplicationNumber( const AStr : string ) : string;
// Used to make strings unique
// This examines the string AStr for trailing '(n)' where
// 'n' is an integer.
// If the (n) part is found, n is incremented, otherwise '(2)' is
// appended to the string.
var
  iLH, iRH, I : integer;
  S           : string;
begin
  Result := AStr;
  iLH    := CharPosBackwards( '(', Result );
  If iLH > 0 then
    begin
    iRH := PosEx( ')', Result, iLH );
    If iRH > 0 then
      begin
      I := StrToIntDef( Copy( Result, iLH+1, iRH-iLH-1 ), 0 );
      If I > 0 then
        begin
        Inc(I);
        S := IntToStr( I );
        Delete( Result, iLH+1, iRH-iLH-1 );
        Insert( S, Result, iLH+1 );
        Exit;
        end;
      end;
    end;

  // Did not increment existing (n), so append it.
  Result := Result + ' (2)';
end;

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.

function AppendDuplicationNumber( const AStr : string ) : string;
// Used to make strings unique
// This examines the string AStr for trailing '(n)' where
// 'n' is an integer.
// If the (n) part is found, n is incremented, otherwise '(2)' is
// appended to the string.
var
  iLH, iRH, I : integer;
  S           : string;
begin
  Result := AStr;
  iLH    := CharPosBackwards( '(', Result );
  If iLH > 0 then
    begin
    iRH := PosEx( ')', Result, iLH );
    If iRH > 0 then
      begin
      I := StrToIntDef( Copy( Result, iLH+1, iRH-iLH-1 ), 0 );
      If I > 0 then
        begin
        Inc(I);
        S := IntToStr( I );
        Delete( Result, iLH+1, iRH-iLH-1 );
        Insert( S, Result, iLH+1 );
        Exit;
        end;
      end;
    end;

  // Did not increment existing (n), so append it.
  Result := Result + ' (2)';
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文