A powerful C# library that automatically extracts base64-encoded images from HTML content and converts them to file references. Perfect for content management systems, email processors, and web applications that need to handle embedded images efficiently.
$ dotnet add package HtmlImageExtractorA powerful C# library that automatically extracts base64-encoded images from HTML content and converts them to file references. Perfect for content management systems, email processors, and web applications that need to handle embedded images efficiently.
When working with rich HTML content (from WYSIWYG editors, email clients, or CMS systems), images are often embedded as base64 data URLs. This creates several issues:
HtmlImageExtractor automatically:
✅ Finds all base64-encoded images in HTML content
✅ Extracts them to separate files (PNG, JPEG, WebP)
✅ Updates HTML to reference external files
✅ Optimizes images for web delivery
✅ Preserves image quality and transparency
✅ Operates entirely in memory for performance
# Package Manager Console
Install-Package HtmlImageExtractor
# .NET CLI
dotnet add package HtmlImageExtractor
# PackageReference (add to .csproj)
<PackageReference Include="HtmlImageExtractor" Version="1.0.0" />
using HtmlImageExtractor;
var htmlContent = @"
<div>
<h1>My Blog Post</h1>
<img src=""data:image/png;base64,iVBORw0KGgo..."" alt=""Screenshot"" />
<p>Regular image: <img src=""/uploads/photo.jpg"" /></p>
</div>";
// Extract images with default settings
var result = HtmlImageExtractor.ExtractImagesFromHtml(htmlContent);
Console.WriteLine($"Extracted {result.ImageFiles.Count} images");
Console.WriteLine($"Modified HTML: {result.ModifiedHtml}");
// Save extracted images to disk
foreach (var image in result.ImageFiles)
{
await File.WriteAllBytesAsync($"images/{image.FileName}", image.FileData);
Console.WriteLine($"Saved: {image.FileName} ({image.FileSizeBytes:N0} bytes)");
}
// Customize extraction settings
var result = HtmlImageExtractor.ExtractImagesFromHtml(
htmlContent: htmlWithEmbeddedImages,
baseUrl: "https://cdn.mysite.com/images", // CDN URL
imageFormat: ImageOutputFormat.WebP, // Modern format
imageNamePrefix: "blog_post_img" // Custom naming
);
// Result HTML will contain:
// <img src="https://cdn.mysite.com/images/blog_post_img_1.webp" />
public async Task<string> ProcessBlogPost(string htmlContent, int postId)
{
// Extract images from rich text editor content
var result = HtmlImageExtractor.ExtractImagesFromHtml(
htmlContent,
baseUrl: $"/content/posts/{postId}/images",
imageFormat: ImageOutputFormat.WebP,
imageNamePrefix: $"post_{postId}_img"
);
// Upload images to storage
foreach (var image in result.ImageFiles)
{
await cloudStorage.UploadAsync($"posts/{postId}/{image.FileName}", image.FileData);
}
return result.ModifiedHtml;
}
public async Task ProcessIncomingEmail(EmailMessage email)
{
var result = HtmlImageExtractor.ExtractImagesFromHtml(
email.HtmlBody,
baseUrl: "https://attachments.mailservice.com",
imageFormat: ImageOutputFormat.Jpeg
);
// Save images as email attachments
foreach (var image in result.ImageFiles)
{
email.Attachments.Add(new Attachment(image.FileName, image.FileData));
}
// Store cleaned HTML
email.HtmlBody = result.ModifiedHtml;
}
public static ExtractResult ExtractImagesFromHtml(
string htmlContent, // Required: HTML to process
string baseUrl = "/images", // Optional: Base URL for images
ImageOutputFormat imageFormat = ImageOutputFormat.Png, // Optional: Output format
string imageNamePrefix = "image" // Optional: Filename prefix
)
Parameters:
htmlContent (string): HTML content containing base64 imagesbaseUrl (string): Base URL where images will be servedimageFormat (ImageOutputFormat): Output format (Png, Jpeg, WebP)imageNamePrefix (string): Prefix for generated filenamesReturns: ExtractResult containing:
ModifiedHtml (string): Updated HTML with file referencesImageFiles (List): Extracted image filespublic class ImageFile
{
public string FileName { get; set; } // Generated filename (e.g., "image_1.png")
public byte[] FileData { get; set; } // Complete file data
public string OriginalMimeType { get; set; } // Source format ("png", "jpeg", etc.)
public ImageOutputFormat OutputFormat { get; set; } // Target format
public long FileSizeBytes { get; set; } // File size in bytes
}
| Format | Use Case | Advantages | File Size |
|---|---|---|---|
| PNG | Screenshots, logos, transparency | Lossless, supports alpha | Large |
| JPEG | Photos, general images | Great compression, universal support | Medium |
| WebP | Modern web apps | Best compression + quality | Small |
The library automatically detects and processes these base64 formats:
data:image/png;base64,...data:image/jpeg;base64,...data:image/jpg;base64,...data:image/webp;base64,...data:image/gif;base64,...data:image/bmp;base64,...Processing 100 base64 images (average 50KB each):
├── Total processing time: ~2.1 seconds
├── Memory usage: ~15MB peak
├── Original HTML size: 6.8MB
└── Optimized HTML size: 0.3MB (95% reduction!)
File size comparison (1000x1000px image):
├── Original base64 in HTML: 1.4MB
├── PNG output: 890KB
├── JPEG output (90% quality): 245KB
└── WebP output: 180KB
The library handles errors gracefully:
try
{
var result = HtmlImageExtractor.ExtractImagesFromHtml(htmlContent);
// Process results
foreach (var image in result.ImageFiles)
{
if (image.FileSizeBytes > maxFileSize)
{
Console.WriteLine($"Warning: {image.FileName} is large ({image.FileSizeBytes:N0} bytes)");
}
}
}
catch (ArgumentException ex)
{
Console.WriteLine($"Invalid input: {ex.Message}");
}
[ApiController]
[Route("api/[controller]")]
public class ContentController : ControllerBase
{
[HttpPost("process")]
public async Task<IActionResult> ProcessContent([FromBody] ContentRequest request)
{
var result = HtmlImageExtractor.ExtractImagesFromHtml(
request.HtmlContent,
baseUrl: Url.Content("~/uploads/"),
imageFormat: ImageOutputFormat.WebP
);
// Save images to wwwroot/uploads
foreach (var image in result.ImageFiles)
{
var path = Path.Combine("wwwroot", "uploads", image.FileName);
await System.IO.File.WriteAllBytesAsync(path, image.FileData);
}
return Ok(new {
Html = result.ModifiedHtml,
ExtractedImages = result.ImageFiles.Count
});
}
}
We welcome contributions! Here's how you can help:
git checkout -b feature/amazing-feature)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)This project is licensed under the MIT License.
Made with ❤️ by Mahammad Azimzada
Transform your HTML content with embedded images into optimized, performant web experiences!