Saturday, January 30, 2021

Get vector (svg) country flag in c#

Simple class to retrieve country flag by its country code. Archive contains normalized vector country flags (svg format) from different sources.

CountryFlags.zip
namespace Example
{
    public static class CountryFlags
    {
        public static IEnumerable<string> AllFlagsResourceNames =&gt; 
            Assembly.GetExecutingAssembly().GetManifestResourceNames().Where(r =&gt; r.Contains("CountryFlags.Images"));

        public static string GetResourceName(string countryCode)
        {
            return $"Examle.CountryFlags.Images.{countryCode.ToLower()}.svg";
        }

        public static Stream GetFlagAsStream(string countryCode)
        {
            return Assembly.GetExecutingAssembly().GetManifestResourceStream(GetResourceName(countryCode));
        }

        public static byte[] GetFlagAsByteArray(string countryCode)
        {
            var stream = GetFlagAsStream(countryCode);
            byte[] result = new byte[stream.Length];
            stream.Read(result, 0, result.Length);
            return result;
        }
    }
}

No comments:

Post a Comment

Working with Freshdesk contacts in powershell

Get all freshdesk contacts in Powershell function Get-LogetoFreshdeskContacts { [CmdletBinding()] param ( [parameter(Ma...