MSBuild task to convert SVG files into raster formats such as PNG and ICO.
$ dotnet add package Zafiro.SvgBuildSvgBuild is an MSBuild task that transforms SVG assets into raster images such as PNG or ICO. It ships as a NuGet package so any project can consume it by adding a single package reference.
Install the NuGet package in the project that needs the raster assets:
<ItemGroup>
<PackageReference Include="SvgBuild" Version="1.0.0" />
</ItemGroup>
Tip: when the package is referenced from multiple projects within a solution, place the
PackageReferencein a sharedDirectory.Build.propsfile so every project sees theConvertSvgtask.
SvgBuild exposes an item called SvgBuildConversion. Each item represents a single conversion that will run before the BeforeBuild phase. The Include attribute is the source SVG, and the OutputPath metadata determines the destination file.
<ItemGroup>
<SvgBuildConversion Include="App/Assets/icon.svg">
<OutputFormat>ico</OutputFormat>
<OutputPath>$(ProjectDir)App.Desktop/Assets/icon.ico</OutputPath>
</SvgBuildConversion>
</ItemGroup>
In an Avalonia solution with a desktop head project (App.Desktop), place the SVG in the shared project (App/Assets/icon.svg) and add the SvgBuildConversion item to the desktop head .csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SvgBuild" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
<SvgBuildConversion Include="..\App\Assets\icon.svg">
<OutputFormat>ico</OutputFormat>
<OutputPath>$(ProjectDir)Assets\icon.ico</OutputPath>
</SvgBuildConversion>
</ItemGroup>
<ItemGroup>
<None Include="Assets\icon.ico" />
</ItemGroup>
</Project>
When the desktop project builds, SvgBuild will create Assets/icon.ico with multiple resolutions so the Win32 window icon looks sharp at every scale.
Use OutputFormat set to png to obtain a raster PNG version of an SVG. The generated file preserves the original aspect ratio and is encoded at maximum quality.
<ItemGroup>
<SvgBuildConversion Include="App/Assets/splash.svg">
<OutputFormat>png</OutputFormat>
<OutputPath>$(ProjectDir)Assets/splash.png</OutputPath>
</SvgBuildConversion>
</ItemGroup>
For advanced scenarios you can invoke the ConvertSvg task explicitly inside a target:
<Target Name="GenerateSplashAssets" BeforeTargets="BeforeBuild">
<ConvertSvg
InputPath="$(ProjectDir)App/Assets/splash.svg"
OutputFormat="png"
OutputPath="$(IntermediateOutputPath)splash.png" />
</Target>
The repository includes an azure-pipelines.yml definition that installs the DotnetDeployer global tool and uses it to build and publish the NuGet package. Publication happens automatically on pushes to main or master, while other branches perform a dry run.
- script: dotnet tool install --global DotnetDeployer.Tool
displayName: Install DotnetDeployer
- pwsh: |
$branch = '$(Build.SourceBranch)'
$isMain = $branch -eq 'refs/heads/main'
$isMaster = $branch -eq 'refs/heads/master'
$shouldPush = $isMain -or $isMaster
Write-Host "Branch: $branch. Will push: $shouldPush"
$arguments = @(
'nuget',
'--api-key', '$(NuGetApiKey)',
'--configuration', 'Release',
'--solution', 'SvgBuild.sln'
)
if (-not $shouldPush) {
$arguments += '--no-push'
}
dotnetdeployer @arguments
displayName: Package and publish with DotnetDeployer
Configure a secret variable called NuGetApiKey in Azure Pipelines (for example through a Variable Group) so the pipeline can authenticate against NuGet.org or your internal feed.
SvgBuild is distributed under the MIT License.