Skip to content

Commit

Permalink
Add support for Windows 7 and 8 lockscreen
Browse files Browse the repository at this point in the history
Also allow easy restore of default configuration
with new lockscreen --restore command line switch
  • Loading branch information
ORelio committed Feb 11, 2018
1 parent f6da61d commit 122976e
Show file tree
Hide file tree
Showing 9 changed files with 491 additions and 36 deletions.
6 changes: 3 additions & 3 deletions README-En.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
=====================================================
==== SpotlightDL v1.1 - By ORelio - Microzoom.fr ====
==== SpotlightDL v1.2 - By ORelio - Microzoom.fr ====
=====================================================

Thanks for dowloading SpotlightDL!
Expand All @@ -10,7 +10,7 @@ SpotlightDL can also define images as wallpaper and system-wide lockscreen image
It is useful in the following use cases:
- Download the whole Spotlight library with maximum image resolution and metadata
- Define Spotlight images as wallpaper, not only on Windows 10 but also on previous versions
- Define Spotlight images as global lock screen: without ads, and without any user being logged in
- Define Spotlight images as global lock screen on Windows 7, 8 and 10, without ads on Windows 10
- Chain SpotlightDL with your own scripts and apps by taking advantage of the url mode

============
Expand Down Expand Up @@ -38,7 +38,7 @@ update-lockscreen
Windows folder and clear the lockscreen cache to force a lockscreen refresh.

restore-lockscreen
This script restores the default lock screen image from Windows 10
This script restores the default lock screen image.
This script must be run as administrator.

generate-manual
Expand Down
6 changes: 3 additions & 3 deletions README-Fr.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
======================================================
==== SpotlightDL v1.1 - Par ORelio - Microzoom.fr ====
==== SpotlightDL v1.2 - Par ORelio - Microzoom.fr ====
======================================================

Merci d'avoir téléchargé SpotlightDL!
Expand All @@ -10,7 +10,7 @@ SpotlightDL peut également définir des images en tant que fond d'écran ou sur
Ce programme est utile dans les cas suivants :
- Télécharger toute la bibliothèque d'images en définition maximale, avec fichiers de métadonnées
- Définir les images en tant que fond d'écran, non seulement sous Windows 10 mais aussi sur les versions précédentes
- Définir les images sur l'écran de verouillage global, sans les publicités, et même lorsqu'aucune session n'est ouverte
- Définir les images sur l'écran de verouillage global sous Windows 7, 8 et 10, sans les publicités sous Windows 10
- Utiliser SpotlightDL dans vos propres scripts et programmes en appelant la fonction récupérant les URL des images

=============
Expand All @@ -37,7 +37,7 @@ update-lockscreen
et vide le cache de l'écran de verouillage afin d'en forcer la mise à jour immédiate.

restore-lockscreen
Ce script restaure l'écran de verouillage par défaut de Windows 10
Ce script restaure l'écran de verouillage par défaut.
Il requiert les droits administrateur.

generate-manual
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SpotlightDL can also define images as wallpaper and system-wide lockscreen image
It is useful in the following use cases:
- Download the whole Spotlight library with maximum image resolution and metadata
- Define Spotlight images as wallpaper, not only on Windows 10 but also on previous versions
- Define Spotlight images as global lock screen: without ads, and without any user being logged in
- Define Spotlight images as global lock screen on Win7/8/10, removing the ads on Windows 10
- Chain SpotlightDL with your own scripts and apps by taking advantage of the url mode

# How to use
Expand Down Expand Up @@ -52,7 +52,7 @@ Spotlight API URL was originally found in this [file](https://github.com/KoalaBR

## Global lock screen

The global lock screen image is stored as `C:\Windows\Web\Screen\img100.jpg`.
The global lock screen image for Windows 8 and 10 is stored as `C:\Windows\Web\Screen\img100.jpg`.
SpotlightDL backups the image as `img200.jpg` if it does not already exists, then overwrite this file.
The lock screen image cache, located at `C:\ProgramData\Microsoft\Windows\SystemData\S-1-5-18\ReadOnly\LockScreen_Z`, must be cleared for the change to take effect.

Expand All @@ -63,6 +63,16 @@ Then, programs running as administrator can overwrite the lockscreen image and c
This way of replacing the lockscreen is basically a C# implementation of [this script](https://www.reddit.com/r/PowerShell/comments/5fglby/powershell_to_set_windows_10_lockscreen/daoepvj/),
avoiding the use of the `takeown` and `iacls` commands which are not reliable due to a [localization issue](http://community.idera.com/powershell/ask_the_experts/f/powershell_for_windows-12/10227/trying-to-make-a-takeown-exe-cmdlet-but-locales-is-causing-a-problem).

Windows 7 support is also implemented through the [OEMBackground](https://www.askvg.com/windows-7-supports-login-screen-customization-without-3rd-party-software-how-to-instructions-inside/) feature:

````
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background]
"OEMBackground"=dword:00000001
````

Then the image has to be placed in `C:\Windows\System32\oobe\info\backgrounds\backgroundDefault.jpg`
Windows 7 enforces a limit of 250 KiB so SpotlightDL will recompress the image to the highest quality fitting in that limit.

# License

SpotlightDL is provided under [CDDL-1.0](http://opensource.org/licenses/CDDL-1.0) ([Why?](http://qstuff.blogspot.fr/2007/04/why-cddl.html)).
Expand Down
6 changes: 3 additions & 3 deletions Scripts/restore-lockscreen.bat
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
@echo off
cd "%~dp0"

:: This script restores the default lock screen image from Windows 10.
:: This script restores the default lock screen image.
:: SpotlightDownloader performs a backup before overwriting the file,
:: so we just need to define the backup file as lockscreen image.
:: so we just need to ask it to restore the backup.

net session > nul 2>&1
if not "%errorlevel%" == "0" (
Expand All @@ -12,4 +12,4 @@ if not "%errorlevel%" == "0" (
exit
)

SpotlightDownloader lockscreen --from-file "%systemroot%\Web\Screen\img200.jpg"
SpotlightDownloader lockscreen --restore
132 changes: 132 additions & 0 deletions SpotlightDownloader/ImageEncoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace SharpTools
{
/// <summary>
/// Wrapper class for generating images
/// By ORelio (c) 2018 - CDDL 1.0
/// <remarks>
/// Jpeg part of this class is written by Pratik and davidwroxy from StackOverflow
/// </remarks>
/// </summary>
public static class ImageEncoder
{
/// <summary>
/// Encode an input image to a Jpeg file below the requested size, trying to achieve maximum possible quality.
/// </summary>
/// <param name="inputPath">Input image path</param>
/// <param name="outputPath">Output jpeg image path</param>
/// <param name="maximumSizeBytes">Maximum size in bytes. If omitted, the Jpeg file is created with maximum available quality</param>
/// <returns>TRUE if thefile was successfully generated</returns>
public static bool CreateJpeg(string inputPath, string outputPath, long maximumSizeBytes = Int64.MaxValue)
{
return CreateJpeg(Image.FromFile(inputPath), outputPath, maximumSizeBytes);
}

/// <summary>
/// Encode an input image to a Jpeg file below the requested size
/// </summary>
/// <param name="inputPath">Input image</param>
/// <param name="outputPath">Output jpeg image path</param>
/// <param name="maximumSizeBytes">Maximum size in bytes. If omitted, the Jpeg file is created with maximum available quality</param>
/// <returns>TRUE if thefile was successfully generated</returns>
public static bool CreateJpeg(Image image, string outputPath, long maximumSizeBytes = Int64.MaxValue)
{
long outputFileSize = Int64.MaxValue;
int quality = 100;
do
{
SaveJpeg(outputPath, image, quality);
outputFileSize = new FileInfo(outputPath).Length;
quality--;
}
while (quality >= 0 && outputFileSize > maximumSizeBytes);
return quality >= 0;
}

/// <summary>
/// Encode an input image to a PNG file
/// </summary>
/// <param name="image">Input image path</param>
/// <param name="outputPath">Output png image path</param>
public static void CreatePng(string inputPath, string outputPath)
{
CreatePng(Image.FromFile(inputPath), outputPath);
}

/// <summary>
/// Encode an input image to a PNG file
/// </summary>
/// <param name="image">Input image</param>
/// <param name="outputPath">Output png image path</param>
public static void CreatePng(Image image, string outputPath)
{
image.Save(outputPath, ImageFormat.Png);
}

/// <summary>
/// Copy an input image file to an output path, converting image if necessary. Existing output file is overwritten.
/// </summary>
/// <param name="inputPath">Input image path</param>
/// <param name="outputPath">Output image path</param>
/// <exception cref="NotSupportedException">Thrown if a non-supported output format is requested and image needs converting.</exception>
public static void AutoCopyImageFile(string inputPath, string outputPath)
{
string intputExt = Path.GetExtension(inputPath).ToLower();
string outputExt = Path.GetExtension(outputPath).ToLower();
if (intputExt == outputExt)
{
File.Copy(inputPath, outputPath, true);
}
else if (outputExt == ".jpg")
{
CreateJpeg(inputPath, outputPath);
}
else if (outputExt == ".png")
{
CreatePng(inputPath, outputPath);
}
else throw new NotSupportedException(String.Format("Output file format '{0}' is not supported.", outputExt));
}

/// <summary>
/// Saves an image as a jpeg image, with the given quality
/// </summary>
/// <param name="path"> Path to which the image would be saved. </param>
/// <param name="quality"> An integer from 0 to 100, with 100 being the highest quality.</param>
/// <seealso>https://stackoverflow.com/a/4161930</seealso>
private static void SaveJpeg(string path, Image img, int quality)
{
if (quality < 0 || quality > 100)
throw new ArgumentOutOfRangeException("quality must be between 0 and 100.");

// Encoder parameter for image quality
EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality);
// JPEG image codec
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
img.Save(path, jpegCodec, encoderParams);
}

/// <summary>
/// Returns the image codec with the given mime type
/// </summary>
/// <seealso>https://stackoverflow.com/a/4161930</seealso>
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
// Get image codecs for all image formats
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

// Find the correct image codec
for (int i = 0; i < codecs.Length; i++)
if (codecs[i].MimeType == mimeType)
return codecs[i];

return null;
}
}
}
Loading

0 comments on commit 122976e

Please sign in to comment.