Skip to content

Instantly share code, notes, and snippets.

@seesharprun
Created April 23, 2025 15:47
Show Gist options
  • Save seesharprun/f01a7f4a7d0c71dcaaffd341ee60cf80 to your computer and use it in GitHub Desktop.
Save seesharprun/f01a7f4a7d0c71dcaaffd341ee60cf80 to your computer and use it in GitHub Desktop.
Convert SVG to a PNG for a favicon
using System.Drawing;
using Svg;
FileInfo source = new(@"source.svg");
FileInfo target = new(@"target.png");
int width = 32;
int height = 32;
float dpi = 96f;
SvgDocument svg = SvgDocument.Open(source.FullName);
svg.Width = width;
svg.Height = height;
using Bitmap bitmap = new(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
bitmap.SetResolution(dpi, dpi);
using Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Transparent);
svg.Draw(graphics);
using FileStream stream = File.Open(target.FullName, FileMode.Create);
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Svg" Version="3.4.7" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment