LoginSignup
3
8

More than 5 years have passed since last update.

【C#版】Windows10のロック画面を壁紙にする

Posted at

Windows10のロック画面の画像がキレイだから壁紙のスライドショーにしたいと思ったの記事がすごくよく、自環境でも実行したかったがJavaの実行環境がなかったため諦めてC#で書き直したので共有します。

コード

PictureMaker.cs
using System;
using System.Drawing;
using System.IO;

namespace StockWallPaper {
    public class PictureMaker {

        private static readonly string _destFolder = 
            Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "LockPictures");

        private static readonly string _windowsLockPictureFolder =
            Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets");

        public static void GetLockPictures() {
            foreach (var picturePath in Directory.GetFiles(_windowsLockPictureFolder)) {
                try {
                    using (var bmp = new Bitmap(Image.FromFile(picturePath))) {
                        if (bmp.Width == 1920) {
                            //コピー先はjpg形式とする。
                            string destFileName = Path.Combine(_destFolder ,Path.GetFileName(picturePath) + ".jpg");
                            File.Copy(picturePath, destFileName, true);
                        }
                    }
                }
                catch (OutOfMemoryException) {
                    //ファイルのロードに失敗する場合は次へ行く。
                }
            }
        }

        public static void MakeLockPictureFolder() {
            if (Directory.Exists(_destFolder) == false) {
                Directory.CreateDirectory(_destFolder);
            }
        }
    }
}

Program.cs
namespace StockWallPaper {
    public class Program {
        public static void Main(string[] args) {
            PictureMaker.MakeLockPictureFolder();
            PictureMaker.GetLockPictures();
        }
    }
}

感想

クラス名とかコードとかゴミだけど、とりあえず動くからよしとしてくださいm(__)m

3
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
8