by
1
6
3,707
4
Top 1% !
Popular
Famous
Easy-to-find
Specified
OpenSource
Popularity: 2933rd place
.NETFramework4.5
Architecturex86_64
Extension.cs
LanguageCSharp

ToDataURL() - Convert a Image HttpPostedFile to DataURI (Base64)

Extension method to convert any image type in a HttpPostedFile to base64 in Data URL format
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/1099951/ToDataURL()---Convert-a-Image-HttpPosted?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
public static string ToDataURL(this HttpPostedFile PostedFile) { try { if (PostedFile.ContentType.ToLower().Contains("image/")) { Stream input = PostedFile.InputStream; int streamLength = Convert.ToInt32(input.Length); byte[] fileData = new byte[streamLength]; input.Read(fileData, 0, streamLength); return "data:" + PostedFile.ContentType.ToLower() + ";base64," + Convert.ToBase64String(fileData.ToArray()); } else { throw new FormatException(PostedFile.FileName + " is not image. (" + PostedFile.ContentType + ")"); } } catch (Exception ex) { throw ex; } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus