/
EugenyCh7
/
VividUI.Net
Обзор
Документация
Войти
/
EugenyCh7
/
VividUI.Net
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
IconsGenerator/Program.cs
102 строки
4 KB
Eugeny Chekmarev
ConvertSvg fix
08 ноя 2024, 17:54
08 ноя 2024, 17:54
6187a23
Код
Авторство
О чём код?
using SkiaSharp; using Svg.Skia; using System.IO.Compression; namespace IconsGenerator { internal class Program { private static void Main(string[] args) { int imageSize = 48; string outputFolder = $"icons-png-{imageSize}x{imageSize}"; SKColor fillColor = SKColors.White; string sourceZip = "icons.zip"; string targetZip = $"{outputFolder}.zip"; if (Directory.Exists(outputFolder)) Directory.Delete(outputFolder, true); Directory.CreateDirectory(outputFolder); ZipFile.ExtractToDirectory(sourceZip, outputFolder); SKShader shader = SKShader.CreateLinearGradient( new SKPoint(0, 0), new SKPoint(imageSize, imageSize), [SKColors.Red, SKColors.Blue], [0, 1], SKShaderTileMode.Clamp); string[] filePaths = Directory.EnumerateFiles(outputFolder, "*.svg", SearchOption.AllDirectories).ToArray(); int number = 0; foreach (string filePath in filePaths) { ++number; Console.Write($"\r{number}/{filePaths.Length} {number * 100.0f / filePaths.Length:F1}%\t"); ConvertSvg( filePath, Path.ChangeExtension(filePath, "png"), imageSize, imageSize, SKEncodedImageFormat.Png, fillColor, shader); File.Delete(filePath); } Console.WriteLine(); if (File.Exists(targetZip)) File.Delete(targetZip); ZipFile.CreateFromDirectory(outputFolder, targetZip); } public static void ConvertSvg(string svgFilePath, string targetFilePath, int width, int height, SKEncodedImageFormat format, SKColor? fillColor = null, SKShader shader = null) { using (var svg = new SKSvg()) { svg.Load(svgFilePath); using (var bitmap = new SKBitmap(width, height)) { using (var canvas = new SKCanvas(bitmap)) { float scaleX = width / svg.Picture.CullRect.Width; float scaleY = height / svg.Picture.CullRect.Height; SKMatrix matrix = SKMatrix.CreateScale(scaleX, scaleY); canvas.Clear(SKColors.Transparent); canvas.DrawPicture(svg.Picture, ref matrix); if (fillColor.HasValue) { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { SKColor pixelColor = bitmap.GetPixel(x, y); canvas.DrawPoint(x, y, fillColor.Value.WithAlpha(pixelColor.Alpha)); } } } if (shader != null) { using (var paint = new SKPaint()) { paint.BlendMode = SKBlendMode.Modulate; paint.Shader = shader; canvas.DrawRect(0, 0, width, height, paint); } } } using (var image = SKImage.FromBitmap(bitmap)) using (var data = bitmap.Encode(format, 100)) using (var stream = File.OpenWrite(targetFilePath)) { data.SaveTo(stream); } } } } } }