屏幕定时截屏并保存图像(bat+powershell+c#实现)

求大神帮编程,然后我复制进 记事本 改后缀名成bat就能用 运行一次就帮我截今天的屏
设定7:00 截屏一次 8:00截屏一次 以此类推 截屏到23:00 每个小时截屏一次 并以时间为名字命名保存在C:\Users\Public\Pictures
越简单越好
谢谢!
cmd 怎么截屏 截屏命令是什么?

---------------------------------------------------------------------------------------

复制粘贴到记事本,另存为xx.bat,编码选ANSI
<# :
cls
@echo off
rem 整点自动截屏
mode con cols=40 lines=8
if "%~1" equ "" (
    start /min "" "%~f0" fk
    exit
)
cd /d "%~dp0"
powershell -sta -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::Default)))"
pause
exit
#>
#截屏图片存放的目录
$folder="C:\ScreenShot";
if(-not (test-path -liter $folder)){[void](md $folder)};
 
$codes=@'
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
public static class ScreenShot{
    public static void Save(string f){
        Image img=new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
        Graphics g=Graphics.FromImage(img);
        g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
        img.Save(f, ImageFormat.Bmp);
    }        
}
'@;
 
Add-Type -TypeDefinition $codes -ReferencedAssemblies 'System.Windows.Forms','System.Drawing';
$n=0;while($true){
    cls
    $now=(get-date).toString('yyyy-MM-dd HH:mm:ss');
    write-host 'Keep this window minimized';
    write-host 'Do not close this window';
    write-host $n.toString();write-host $now;
    $m=[regex]::match($now, '00:0[0-9]$');
    if($m.Success){
        $newfile=$folder.trimend('\')+'\'+($now -replace '[-\s:]|\d\d$','')+'00.bmp';
        if(-not (test-path -liter $newfile)){$n++;[ScreenShot]::Save($newfile);};
    };
    start-sleep -Seconds 1;
}

 

===============================================================

 个人使用

1)调整一下换行,增加了一些注释

<# :
cls
@echo off
rem 整点自动截屏
mode con cols=40 lines=8
if "%~1" equ "" (
    start /min "" "%~f0" fk
    exit
)
cd /d "%~dp0"
powershell -sta -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::Default)))"
pause
exit
#>
#截屏图片存放的目录
$folder="D:\ScreenShot";
if(-not (test-path -liter $folder)){[void](md $folder)};
 
$codes=@'
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
public static class ScreenShot{
    public static void Save(string f){
        Image img=new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
        Graphics g=Graphics.FromImage(img);
        g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
        img.Save(f, ImageFormat.Bmp);
    }        
}
'@;
 
Add-Type -TypeDefinition $codes -ReferencedAssemblies 'System.Windows.Forms','System.Drawing';
$n=0;
#不需要循环,可以把while去掉,只保留方法体
while($true){
    cls
    $now=(get-date).toString('yyyy-MM-dd HH:mm:ss');
    write-host 'Keep this window minimized';
    write-host 'Do not close this window';
    write-host 'ScreenShot count:'$n.toString();
    write-host $now;
    #使用正则匹配正点时间
    $m=[regex]::match($now, '17:00:0[0-9]$');#下面的判断可以使用$True,或者$m.Success
    if($m.Success){
        $newfile=$folder.trimend('\')+'\'+($now -replace '[-\s:]|\d\d$','')+'00.bmp';
        $newfile=$folder.trimend('\')+'\'+($now -replace '[-\s:]','')+'.bmp';
        if(-not (test-path -liter $newfile)){$n++;[ScreenShot]::Save($newfile);};
    };
    start-sleep -Seconds 1;
}

 

 

出处:https://zhidao.baidu.com/question/1515575716199249700.html

posted on 2025-11-07 16:15  jack_Meng  阅读(0)  评论(0)    收藏  举报

导航