Skip to content

Instantly share code, notes, and snippets.

@Grabacr07
Last active May 10, 2016 11:11
Show Gist options
  • Save Grabacr07/cd86257f9ad8b0c210d11e5dd6c93ce6 to your computer and use it in GitHub Desktop.
Save Grabacr07/cd86257f9ad8b0c210d11e5dd6c93ce6 to your computer and use it in GitHub Desktop.
Windows Spotlight の画像を縦横仕分けして保存するやつ (LINQPad 向け)
void Main()
{
var sourcePath = Environment.ExpandEnvironmentVariables(@"%LOCALAPPDATA%\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets");
var destinationPath = @"*** Your wallpaper folder ***";
var sourceDirectory = new DirectoryInfo(sourcePath);
if (!sourceDirectory.Exists)
{
$"Directory not found.".Dump();
return;
}
var destinationDirectory = new DirectoryInfo(destinationPath);
destinationDirectory.Create();
new Hyperlinq(() => Process.Start("explorer", sourcePath), sourcePath).Dump("Source");
new Hyperlinq(() => Process.Start("explorer", destinationPath), destinationPath).Dump("Destination");
var landscape = destinationDirectory.CreateSubdirectory("Landscape");
var portrait = destinationDirectory.CreateSubdirectory("Portrait");
var pics = sourceDirectory.EnumerateFiles().Where(x => x.Length > 100000).ToArray();
var progress = new ProgressBar("Copying:", true, 0, pics.Length).Dump();
// アプリのアイコン等ゴミが混ざるので、100 KB 以上を引っ張ってくるようにしてみるテスト
foreach (var file in sourceDirectory.GetFiles().Where(x => x.Length > 100000))
{
progress.Caption = $"Copying: ({progress.Value} of {pics.Length})";
using (var image = Image.FromFile(file.FullName))
{
if (image.Size.Width >= 1920)
{
var path = Path.Combine(landscape.FullName, $"{file.Name}.jpg");
if (!File.Exists(path))
{
$"Landscape ({image.Size.Width} x {image.Size.Height}) {path}".Dump();
file.CopyTo(path);
Util.RawHtml($"<img src='{file.FullName}' style='width: 25%; height: 25%' />").Dump();
}
}
else if (image.Size.Height >= 1920)
{
var path = Path.Combine(portrait.FullName, $"{file.Name}.jpg");
if (!File.Exists(path))
{
$"Portrait ({image.Size.Width} x {image.Size.Height}) {path}".Dump();
file.CopyTo(path);
Util.RawHtml($"<img src='{file.FullName}' style='width: 25%; height: 25%' />").Dump();
}
}
}
++progress.Value;
}
}
public class ProgressBar : Util.ProgressBar
{
private int _min;
public int Min
{
get { return this._min; }
set
{
this._min = value;
this.Refresh();
}
}
private int _max;
public int Max
{
get { return this._max; }
set
{
this._max = value;
this.Refresh();
}
}
private int _value;
public int Value
{
get { return this._value; }
set
{
this._value = value;
this.Refresh();
}
}
public ProgressBar(string caption = "", bool hideWhenCompleted = false, int min = 0, int max = 100)
: base(caption, hideWhenCompleted)
{
this.Min = min;
this.Max = max;
}
public void Refresh()
{
this.Fraction = this.Max == 0
? 0
: Math.Max(0, Math.Min((double)(this.Value - this.Min) / this.Max, 1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment