Advertisement
scottgalPastes

Untitled

Dec 8th, 2022
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Threading.Tasks;
  6. using Avalonia.Controls;
  7. using SixLabors.ImageSharp;
  8. using SixLabors.ImageSharp.Formats.Gif;
  9. using SixLabors.ImageSharp.Processing;
  10. using SixLabors.ImageSharp.Processing.Processors.Quantization;
  11. using File = System.IO.File;
  12. using Image = SixLabors.ImageSharp.Image;
  13.  
  14. namespace Giffinator.Client;
  15.  
  16. public partial class MainWindow : Window
  17. {
  18.     public MainWindow()
  19.     {
  20.         InitializeComponent(); Loaded += MainWindow_Loaded;
  21.  
  22.  
  23.     }
  24.  
  25.     private async void MainWindow_Loaded(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
  26.     {
  27.         await LoadGifs();
  28.     }
  29.  
  30.     private List<GifRecord> Gifs { get; set; } = new List<GifRecord>();
  31.  
  32.     private record GifRecord(string FullPath, long SizeInBytes, string ThumbNailPath);
  33.  
  34.     private async Task LoadGifs()
  35.     {
  36.         var gifPath = @"F:\Gifs\";
  37.  
  38.         var thumbPath = Path.GetFullPath(AppContext.BaseDirectory + "Thumbs");
  39.         if (!Directory.Exists(thumbPath)) Directory.CreateDirectory(thumbPath);
  40.         var gifPathStrings = new DirectoryInfo(gifPath).GetFiles("*.gif", SearchOption.AllDirectories);
  41.         var parelellOptions = new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount };
  42.         await Parallel.ForEachAsync(gifPathStrings, parelellOptions, async (info, token) =>
  43.         {
  44.             var fileName = DateTime.Now.ToFileTimeUtc() + info.Name;
  45.             fileName = Path.Combine(thumbPath, fileName);
  46.             if (File.Exists(fileName)) return;
  47.             var fs = File.OpenRead(info.FullName);
  48.             Image? img = null;
  49.             try
  50.             {
  51.                 img = await Image.LoadAsync(fs, token);
  52.                 {
  53.  
  54.                     img.Mutate(x => x.Resize(400, 0));
  55.                     var encoder = new GifEncoder()
  56.                     {
  57.                         ColorTableMode = GifColorTableMode.Global,
  58.                         Quantizer = new OctreeQuantizer(new QuantizerOptions() { MaxColors = 32 })
  59.                     };
  60.  
  61.                     await img.SaveAsync(fileName, encoder, token);
  62.                 }
  63.                 var fileInfo = new GifRecord(info.FullName, info.Length, fileName);
  64.                 Gifs.Add(fileInfo);
  65.             }
  66.             catch (UnknownImageFormatException e)
  67.             {
  68.                 Debug.WriteLine(e);
  69.  
  70.             }
  71.             catch (NotSupportedException e)
  72.             {
  73.                 Debug.WriteLine(e);
  74.  
  75.             }
  76.             finally
  77.             {
  78.  
  79.                 img?.Dispose();
  80.             }
  81.  
  82.         });
  83.  
  84.  
  85.     }
  86.  
  87.  
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement