Skip to content

mikenakis/MikeNakis.SvgConvert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MikeNakis.SvgConvert
Converts SVG files to other file formats. (PNG and ICO.)

Build Build

MikeNakis.SvgConvert is a cross-platform command-line utility (packaged as a dotnet tool) that can be used to convert SVG files to other file formats.

Supported formats:

  • PNG
  • ICO (PNG-based Windows Icon)

The generated windows-icon file has multiple resolutions, transparent background, and is PNG-based, (a feature supported by Windows since Vista,) so it can have semi-transparent pixels instead of being limited to pixels that are either fully transparent or fully opaque. In other words, the generated icon looks perfect.

How it works

MikeNakis.SvgConvert makes use of wieslawsoltes/Svg.Skia (GitHub, NuGet) for loading SVG files and rendering them into in-memory images, and SkiaSharp (GitHub, NuGet) for saving in-memory images to PNG. MikeNakis.SvgConvert implements the logic for creating a multi-image ICO file from a series of PNG images based on information found in Wikipedia: ICO (file_format).

MikeNakis.SvgConvert does not use the GitHub - svg-net/SVG library anymore, because it is windows-only.

Purpose

The main purpose of MikeNakis.SvgConvert is to allow us to create the icon of our dotnet application as an SVG file and to have the corresponding ICO file automatically generated during build.

Advantages

  1. Our icons are expressed in vector form, so they are infinitely scalable without losing quality.
  2. We do not have to store any raster images (which are binary) in source control.
  3. SVG files are text files, so each time we modify them our source control system can show us precisely what changes we made.
  4. By incorporating SvgConvert in our build process, the conversion from SVG to ICO takes place only if the SVG file has changed.
  5. Each time the SVG file changes, the corresponding ICO file is automatically generated without the need for manual steps.

How to use MikeNakis.SvgConvert

  1. Create your application icon as an SVG file and save it with an .ico.svg extension in your project directory. For example, MyApp.ico.svg.

  2. In your project file, specify that your application icon is located in $(BaseIntermediateOutputPath), and that it has an .ico extension. For example:

    <Project ...>
    	<PropertyGroup>
    		<ApplicationIcon>$(BaseIntermediateOutputPath)$(AssemblyName).ico</ApplicationIcon>
    		...
  3. Further down in your project file, add the following code:

     <!-- MikeNakis.SvgConvert boilerplate -->
     <ItemGroup>
    	<SvgConvertInput Include="**/*.ico.svg" />
    	<AvailableItemName Include="SvgConvertInput" />
     </ItemGroup>
     <Target Name="SvgConvertTarget" BeforeTargets="BeforeBuild" Inputs="@(SvgConvertInput)" Outputs="@(SvgConvertInput->'$(BaseIntermediateOutputPath)%(Filename)')">
    	<Message Importance="High" Text="MikeNakis.SvgConvert %(SvgConvertInput.FullPath) $(BaseIntermediateOutputPath)%(SvgConvertInput.Filename) ==============================================================================" />
    	<Exec WorkingDirectory="$(MSBuildProjectDirectory)" Command="cmd /c MikeNakis.SvgConvert windows-ico &quot;%(SvgConvertInput.FullPath)&quot; &quot;$(BaseIntermediateOutputPath)%(SvgConvertInput.Filename)&quot;" />
     </Target>
    

Note that you will probably have to modify the cmd /c MikeNakis.SvgConvert part to point to the location where you have the MikeNakis.SvgConvert executable. Other than that, the MikeNakis.SvgConvert boilerplate code is the same for all of your projects.

That's it. From that moment on, every time you modify an .ico.svg file in your project, the corresponding .ico file will be automatically generated during build.

Note: the above is just one way that I found to make it work. There almost certainly exist better ways. If you know of a better way, please let me know.

Important notice about the Microsoft Visual Studio Icon Editor

If you use Visual Studio to open an ICO file generated by MikeNakis.SvgConvert, you might notice some grotesque artifacts on the fringes between transparent and opaque pixels: Fully transparent pixels look okay, fully opaque pixels also look okay, but semi-transparent pixels appear to be rendered against a black background instead of a transparent background, which of course looks horrible.

Rest assured that these artifacts are not actually present in the ICO file generated by MikeNakis.SvgConvert; they are only visible in the Visual Studio Icon Editor, and are exclusively due to a rendering malfunction of the Visual Studio Icon Editor.

This is happening because Microsoft only applied a quick and dirty patch to their Icon Editor so that it just barely copes with PNG-based ICO files, (which, mind you, were introduced with Windows Vista back in 2006,) instead of properly fixing their Icon Editor to actually support PNG-based ICO files.

Important notice about fonts

Your SVG file may be making use of fonts that are unknown to Mikenakis.SvgConvert. If this happens, then text in your SVG file will not transfer as intended to the ICO file.

One way to prevent this from happening is to make use of the "convert text to path" command of your SVG editor, so that your SVG file contains no text elements.

License

Published under the MIT license. Do whatever you want with it.

More information: see LICENSE.md

Coding style

This project uses my very own™ coding style.

More information: see michael.gr - On Coding Style

TO DO


Cover image by michael.gr including parts of "convert" by nangicon from the Noun Project used under CC BY 3.0.

About

Converts SVG files to other file formats.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published