Created
June 10, 2024 15:25
-
-
Save bjoerntx/285bfa98d33380701981876f7578b036 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static List<ImageData> GetImageBase64Strings(string html) | |
{ | |
var imageDatas = new List<ImageData>(); | |
var regex = new Regex("<img[^>]+?src=[\"']data:image/(?<format>[^;]+);base64,(?<data>[^\"']+)[\"'][^>]*>", RegexOptions.IgnoreCase); | |
foreach (Match match in regex.Matches(html)) | |
{ | |
var format = match.Groups["format"].Value; | |
var base64Content = match.Groups["data"].Value; | |
var extension = GetFileExtension(format); | |
imageDatas.Add(new ImageData | |
{ | |
Base64Content = base64Content, | |
Format = format, | |
Extension = extension | |
}); | |
} | |
return imageDatas; | |
} | |
static string GetFileExtension(string format) | |
{ | |
return format.ToLower() switch | |
{ | |
"jpeg" => ".jpg", | |
"jpg" => ".jpg", | |
"png" => ".png", | |
"gif" => ".gif", | |
"bmp" => ".bmp", | |
"webp" => ".webp", | |
"svg+xml" => ".svg", | |
"tiff" => ".tif", | |
"x-icon" => ".ico", | |
_ => $".{format}" | |
}; | |
} | |
public class ImageData | |
{ | |
public string Base64Content { get; set; } | |
public string Format { get; set; } | |
public string Extension { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment