Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created December 8, 2022 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/798e4e28c6bac4cb251328e08ed4c302 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/798e4e28c6bac4cb251328e08ed4c302 to your computer and use it in GitHub Desktop.
Convert HTML to JPG in C# | C# HTML to JPG | HTML to JPG
// This code example demonstrates how to convert an HTML file to a JPG image.
// Load input HTML file
var document = new HTMLDocument(@"C:\Files\sample.html");
// Initialize ImageSaveOptions
var options = new ImageSaveOptions();
// Specify image format as Jpeg
options.Format = ImageFormat.Jpeg;
// Convert HTML to JPG
Converter.ConvertHTML(document, options, @"C:\Files\output.jpg");
// This code example demonstrates how to generate HTML string output in a JPG image.
string htmlString = @"<style>
.st
{
color: green;
}
</style>
<div id=id1>Aspose.Html rendering Text in Black Color</div>
<div id=id2 class=''st''>Aspose.Html rendering Text in Green Color</div><div id=id3 class=''st'' style='color: blue;'>Aspose.Html rendering Text in Blue Color</div>
<div id=id3 class=''st'' style='color: red;'><font face='Arial'>Aspose.Html rendering Text in Red Color</font></div>";
// Initialize ImageSaveOptions
var options = new ImageSaveOptions(ImageFormat.Jpeg);
// Invoke the ConvertHTML method to convert the HTML code to PDF
Converter.ConvertHTML(htmlString, ".", options, @"C:\Files\HtmlStringToJpg.jpg");
// This code example demonstrates how to convert an HTML file to JPG images with image save options.
// Load input HTML file
var document = new HTMLDocument(@"C:\Files\sample.html");
// Initialize ImageSaveOptions
var options = new ImageSaveOptions(ImageFormat.Jpeg);
options.SmoothingMode = SmoothingMode.HighQuality;
options.HorizontalResolution = 200;
options.VerticalResolution = 200;
options.BackgroundColor = Color.AliceBlue;
// Set page size and margings
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(600, 800), new Margin(10, 10, 10, 10));
// Convert HTML to JPG
Converter.ConvertHTML(document, options, @"C:\Files\output_saveOptions.jpg");
// This code example demonstrates how to convert a live webpage to a JPG image.
// URL
Url url = new Url("https://docs.aspose.com/html/net/");
// Initialize ImageSaveOptions
var options = new ImageSaveOptions(ImageFormat.Jpeg);
// Convert the HTML to JPG
Converter.ConvertHTML(url, options, @"C:\Files\outputFromURL.jpg");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment