简读

提取Windows聚焦图片(Spotlight)

2017-10-10  本文已影响0人  SparkOfLife

前言

Windows聚焦(Windows Spotlight)是Windows 10里边新增的一个锁屏壁纸功能,它会自动下载并随机更换锁屏壁纸。Windows聚焦中壁纸往往非常精美震撼,值得大家收藏。可这些壁纸都放在你电脑上的缓存文件夹中并且没有后缀名,需加上".jpg"才能预览。如下图所示:

C:\Users\你的用户名\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\AssetsC:\Users\你的用户名\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets
路径:C:\Users\你的用户名\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets

如果你喜欢一张Windows聚焦中的壁纸,需要将所有壁纸都加上后缀名才能找到你想要的那张壁纸,效率太低。我们可以利用Windows自带的PowerShell功能自动将这些图片加上后缀并保存在"图片"文件夹中。

仅保存Windows聚焦图片

代码如下

# 将复制出来的缓存图片保存在下面的文件夹  
add-type -AssemblyName System.Drawing  
New-Item "$($env:USERPROFILE)\Pictures\Spotlight" -ItemType directory -Force;  
New-Item "$($env:USERPROFILE)\Pictures\Spotlight\CopyAssets" -ItemType directory -Force;  
New-Item "$($env:USERPROFILE)\Pictures\Spotlight\Horizontal" -ItemType directory -Force;  
New-Item "$($env:USERPROFILE)\Pictures\Spotlight\Vertical" -ItemType directory -Force;  
 
# 将横竖图片分别复制到对应的两个文件夹  
foreach($file in (Get-Item "$($env:LOCALAPPDATA)\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets\*"))  
{  
    if ((Get-Item $file).length -lt 100kb) { continue }  
    Copy-Item $file.FullName "$($env:USERPROFILE)\Pictures\Spotlight\CopyAssets\$($file.Name).jpg";  
}  
  
foreach($newfile in (Get-Item "$($env:USERPROFILE)\Pictures\Spotlight\CopyAssets\*"))  
{  
    $image = New-Object -comObject WIA.ImageFile;  
    $image.LoadFile($newfile.FullName);  
    if($image.Width.ToString() -eq "1920"){ Move-Item $newfile.FullName "$($env:USERPROFILE)\Pictures\Spotlight\Horizontal" -Force; }  
    elseif($image.Width.ToString() -eq "1080"){ Move-Item $newfile.FullName "$($env:USERPROFILE)\Pictures\Spotlight\Vertical" -Force; }  
}  
 
 

新建一个文本文档,将上述代码粘贴进去,保存后缀为.ps1,命名为GetWallPaperFromSpotlight.ps1,然后右键"使用PowerShell运行"就可以发现所有聚焦图片被保存在你自己的用户图片文件夹下的Spotlight文件夹中。

保存并设为Windows桌面壁纸

代码如下

# 将复制出来的缓存图片保存在下面的文件夹  
add-type -AssemblyName System.Drawing  
New-Item "$($env:USERPROFILE)\Pictures\Spotlight" -ItemType directory -Force;  
New-Item "$($env:USERPROFILE)\Pictures\Spotlight\CopyAssets" -ItemType directory -Force;  
New-Item "$($env:USERPROFILE)\Pictures\Spotlight\Horizontal" -ItemType directory -Force;  
New-Item "$($env:USERPROFILE)\Pictures\Spotlight\Vertical" -ItemType directory -Force;  
 
# 将横竖图片分别复制到对应的两个文件夹  
foreach($file in (Get-Item "$($env:LOCALAPPDATA)\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets\*"))  
{  
    if ((Get-Item $file).length -lt 100kb) { continue }  
    Copy-Item $file.FullName "$($env:USERPROFILE)\Pictures\Spotlight\CopyAssets\$($file.Name).jpg";  
}  
  
foreach($newfile in (Get-Item "$($env:USERPROFILE)\Pictures\Spotlight\CopyAssets\*"))  
{  
    $image = New-Object -comObject WIA.ImageFile;  
    $image.LoadFile($newfile.FullName);  
    if($image.Width.ToString() -eq "1920"){ Move-Item $newfile.FullName "$($env:USERPROFILE)\Pictures\Spotlight\Horizontal" -Force; }  
    elseif($image.Width.ToString() -eq "1080"){ Move-Item $newfile.FullName "$($env:USERPROFILE)\Pictures\Spotlight\Vertical" -Force; }  
}  
 
# 壁纸设置函数  
function Set-Wallpaper  
{  
    param(  
        [Parameter(Mandatory=$true)]  
        $Path,  
   
        [ValidateSet('Center', 'Stretch')]  
        $Style = 'Center'  
    )  
   
    Add-Type @"  
using System;  
using System.Runtime.InteropServices;  
using Microsoft.Win32;  
namespace Wallpaper  
{  
public enum Style : int  
{  
Center, Stretch  
}  
public class Setter {  
public const int SetDesktopWallpaper = 20;  
public const int UpdateIniFile = 0x01;  
public const int SendWinIniChange = 0x02;  
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]  
private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);  
public static void SetWallpaper ( string path, Wallpaper.Style style ) {  
SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );  
RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);  
switch( style )  
{  
case Style.Stretch :  
key.SetValue(@"WallpaperStyle", "2") ;  
key.SetValue(@"TileWallpaper", "0") ;  
break;  
case Style.Center :  
key.SetValue(@"WallpaperStyle", "1") ;  
key.SetValue(@"TileWallpaper", "0") ;  
break;  
}  
key.Close();  
}  
}  
}  
"@  
   
    [Wallpaper.Setter]::SetWallpaper( $Path, $Style )  
}  
   
  
$filePath = "$($env:USERPROFILE)\Pictures\Spotlight\Horizontal\*"  
$file = Get-Item -Path $filePath | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1  
Set-Wallpaper -Path $file.FullName    
# echo $file.FullName  
  
Remove-Item "$($env:USERPROFILE)\Pictures\Spotlight\CopyAssets\*";  
#pause  

同样地,新建一个文本文档,将上述代码粘贴进去,保存后缀为.ps1,命名为SetWallPaperFromSpotlight.ps1,然后右键"使用PowerShell运行"就可以看到桌面背景已经被换成了Windows聚焦图片。

每天自动提取并设置为Windows桌面壁纸

利用Windows自带的任务计划程序和上面的的PowerShell脚本就可以实现。由于有些Windows聚焦图片我不是很喜欢,所以我不想自动更换壁纸。因此这一部分我并没有尝试实现。感兴趣的同学可以参考这篇文章







参考:http://blog.csdn.net/anymake_ren/article/details/51125609

上一篇下一篇

猜你喜欢

热点阅读