Created
April 23, 2025 15:47
-
-
Save seesharprun/f01a7f4a7d0c71dcaaffd341ee60cf80 to your computer and use it in GitHub Desktop.
Convert SVG to a PNG for a favicon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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