DEV Community

Shahzad Ashraf
Shahzad Ashraf

Posted on

How to Convert HTML to Image in C# .NET

Transforming HTML pages into static images is a crucial feature for web developers who wish to archive dynamic web content, create visual previews, or automate workflows for converting web content to images. With the GroupDocs.Conversion Cloud .NET SDK, you can effortlessly convert HTML into image formats such as JPG, PNG, or BMP using just a few lines of C# code—eliminating the need for manual rendering or extra libraries.

This robust Cloud-based .NET API streamlines intricate rendering tasks and enables developers to embed HTML-to-image capabilities directly into their web, desktop, or cloud applications. The SDK guarantees precise layout retention, high-quality output, and uniform results across various platforms. You can convert entire HTML documents, including styles and embedded resources, into sharp images that are ideal for visual documentation, archiving, or distribution.

By incorporating the GroupDocs.Conversion Cloud SDK for .NET, developers can fully automate the conversion of HTML to images with minimal configuration. Its REST API architecture offers cross-platform adaptability, while its ease of use allows teams to boost productivity without the concern of local dependencies or issues related to server-side rendering. Begin converting HTML to images programmatically today and discover the advantages of modern document processing powered by GroupDocs Cloud.

Click this link for the comprehensive guide.

Code example:

using System;
using System.IO;
using GroupDocs.Conversion.Cloud.Sdk.Api;
using GroupDocs.Conversion.Cloud.Sdk.Client;
using GroupDocs.Conversion.Cloud.Sdk.Model;
using GroupDocs.Conversion.Cloud.Sdk.Model.Requests;

namespace HTMLtoImageConversion
{
    class Program
    {
        static void Main(string[] args)
        {

            // Configure API credentials
            string MyAppID = "your-app-id";
            string MyAppSecret = "your-app-secret";

            var config = new Configuration(MyAppID, MyAppSecret);
            var convertApi = new ConvertApi(config);

            // Prepare conversion settings
            var convertSettings = new ConvertSettings
            {
                // Source file path in cloud storage
                FilePath = "SampleFiles/source.html",
                // Set output format
                Format = "jpeg",
                ConvertOptions = new JpgConvertOptions
                {
                    FromPage = 1,
                    PagesCount = 1,
                },
                // Output file path in cloud storage
                OutputPath = "conversion/converted.jpg",
            };

            try
            {
                // Perform HTML to JPG image conversion
                var request = new ConvertDocumentRequest(convertSettings);
                var response = convertApi.ConvertDocument(request);

                Console.WriteLine(response != null
                    ? "HTML to JPG conversion was successful!"
                    : "HTML to JPG conversion failed.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred: " + ex.Message);
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)