Convert image url to base64 #453

Closed
opened 2026-01-29 14:37:09 +00:00 by claunia · 3 comments
Owner

Originally created by @fairking on GitHub (Apr 9, 2021).

Is there any option to tell Markdip instead of rendering the image with the url, include its base64.

I am building html on the server side. I am not sure if it's a good approach (no image caching etc.) but the client would not have an access to any md nor image files. So I'm thinking whether it's possible that Markdig can translate images with urls to the base64 representation. I am not expecting a huge image file sizes (up to 5Kb).

Eg.:
index.md:

![Picture 1](images/img1.png)

index.html:

<img alt="Picture 1" src="data:image/png;base64,iVBORw0KGgoAAA
ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4
//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU
5ErkJggg==">

Very appreciate any help. Thanks.

Originally created by @fairking on GitHub (Apr 9, 2021). Is there any option to tell Markdip instead of rendering the image with the url, include its base64. I am building html on the server side. I am not sure if it's a good approach (no image caching etc.) but the client would not have an access to any md nor image files. So I'm thinking whether it's possible that Markdig can translate images with urls to the base64 representation. I am not expecting a huge image file sizes (up to 5Kb). Eg.: `index.md:` ``` ![Picture 1](images/img1.png) ``` `index.html:` ``` <img alt="Picture 1" src="data:image/png;base64,iVBORw0KGgoAAA ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4 //8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU 5ErkJggg=="> ``` Very appreciate any help. Thanks.
Author
Owner

@fairking commented on GitHub (Apr 9, 2021):

By the way I know I can do that manually by replacing all images with it's base64 after the html convertion. But wondering if there some option already exists.

@fairking commented on GitHub (Apr 9, 2021): By the way I know I can do that manually by replacing all images with it's base64 after the html convertion. But wondering if there some option already exists.
Author
Owner

@MihaZupan commented on GitHub (Apr 9, 2021):

This doesn't exist in Markdig as it's out of scope (external IO).

You can do the conversion yourself, something like:

MarkdownDocument doc = Markdown.Parse(markdown);

foreach (LinkInline link in doc.Descendants<LinkInline>())
{
    if (link.IsImage)
    {
        link.Url = await GetBase64ImageAsync(link.Url);
    }
}

string html = doc.ToHtml();
@MihaZupan commented on GitHub (Apr 9, 2021): This doesn't exist in Markdig as it's out of scope (external IO). You can do the conversion yourself, something like: ```c# MarkdownDocument doc = Markdown.Parse(markdown); foreach (LinkInline link in doc.Descendants<LinkInline>()) { if (link.IsImage) { link.Url = await GetBase64ImageAsync(link.Url); } } string html = doc.ToHtml(); ```
Author
Owner

@fairking commented on GitHub (Apr 12, 2021):

Thanks a lot for a very smart solution. Bellow you can find a GetBase64ImageAsync(link.Url) for those who needs it:

        private async Task<string> GetBase64ImageAsync(string imageUrl)
        {
            var imageBytes = await File.ReadAllBytesAsync(imageUrl);

            string base64String = Convert.ToBase64String(imageBytes);

            if (!new FileExtensionContentTypeProvider().TryGetContentType(imageUrl, out string contentType))
                throw new NotSupportedException($"Image file type '{Path.GetExtension(imageUrl)}' is not supported.");

            return $"data:{contentType};base64,{base64String}";
        }
@fairking commented on GitHub (Apr 12, 2021): Thanks a lot for a very smart solution. Bellow you can find a `GetBase64ImageAsync(link.Url)` for those who needs it: ``` private async Task<string> GetBase64ImageAsync(string imageUrl) { var imageBytes = await File.ReadAllBytesAsync(imageUrl); string base64String = Convert.ToBase64String(imageBytes); if (!new FileExtensionContentTypeProvider().TryGetContentType(imageUrl, out string contentType)) throw new NotSupportedException($"Image file type '{Path.GetExtension(imageUrl)}' is not supported."); return $"data:{contentType};base64,{base64String}"; } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#453