Create image object from base64 and save as file #1571
Unanswered
HypeillingerChillinger
asked this question in
Q&A
Replies: 2 comments 3 replies
-
You’d have to get the bytes from the string and pass to a stream. |
Beta Was this translation helpful? Give feedback.
3 replies
-
I wrote a small helper class to load an image from base64 string (as return by public partial class ImageSharpHelper
{
[GeneratedRegex(@"^data:(image\/[a-zA-Z]+);base64,(.*)$")]
private static partial Regex Base64Regex();
public static Image FromBase64String(string base64String)
{
var decodedString = Uri.UnescapeDataString(base64String);
var match = Base64Regex().Match(decodedString);
if (!match.Success)
{
throw new ArgumentException("The current image is not in expected format.");
}
/* var mimeType = match.Groups[1].Value; */
var base64Data = match.Groups[2].Value;
var bytes = Convert.FromBase64String(base64Data);
return Image.Load(bytes);
}
} Usage: using var loadedImage = ImageSharpHelper.FromBase64String(base64ImageString); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
How can I create an ImageSharp Image object from a base64 string?
I only found ImageExtensions.ToBase64String but not sth. like FromBase64String
Use a MemoryStream with Image.Load?
Beta Was this translation helpful? Give feedback.
All reactions