/Base64Image

An MVC Razor extensions for easily adding images as base64 encoded

Primary LanguageC#

Base64Image


an easy way to add images as a base64 encoded strings in your MVC Razor pages.

Examples:

Add the Base64Image package to your Web project

Install-Package Base64Image

Add a using to Base64Image namespace in your .cshtml view

@using Base64Image

Add the following line to your .cshtml || .aspx view, replace the base64 string with your image base64 string

Razor syntax

@Html.Base64ImageElement("iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg == ")

web forms syntax

<%= Html.Base64ImageElement("iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg == ") %>

To add an image from a file on your web server use the following example

@Html.Base64ImageFromFile("/Content/PIFN0.jpg")

replace PIFn0.jpg with your file relative path and name


For custom HTML attributes supply a dictionary with custom attirbutes

@Html.Base64ImageFromFile("/Content/code-flat.png", new Dictionary<string, string> { { "class", "dottedBorder" }, { "id", "imageIdX" } })

Check out the Demo web project in my repository for more examples.

You may need to add assembly binding redirects if you end up with conflicting MVC dll's versions.