Search code examples
c#base64url-encodingcloudinary

How do I convert the following remote URL to a base64 safe URL with C#?


I want to fetch remote images and do some image transformation specifically an overlay over an image with cloudinary. But cloudinary only supports 64base safe urls to overlay images. Lets say I have this following remote image URL.

https://scontent.xx.fbcdn.net/v/t1.0-9/15665479_1260320054027269_4201232212927955955_n.jpg?oh=ee01f2ec47b2e972bc12f99d988db241&oe=5946A159

How can I encode it to a 64base url? I have been through many SO questions related to the question but none of them were helped me to solve the issue.


Solution

  • In case you need to encode both URL and Image (may not be what you need, but might be useful sometime)

    string url = "https://scontent.xx.fbcdn.net/v/t1.0-9/15665479_1260320054027269_4201232212927955955_n.jpg?oh=ee01f2ec47b2e972bc12f99d988db241&oe=5946A159";
    
    string encodedUrl = Convert.ToBase64String(Encoding.Default.GetBytes(url));
    
    using (var client = new WebClient())
    {
        byte[] dataBytes = client.DownloadData(new Uri(url));
        string encodedFileAsBase64 = Convert.ToBase64String(dataBytes);
    }