Save image got blurry but clear in pdf

We want to convert a html string into pdf and image. We have done this by following these piece of code. Pdf output is fine but the image output is very blurry. How do we make the image output very clear (Same as Pdf)?


         // Add the html string from here https://pastebin.com/bYZd50ME

            string htmlString = "";

            var htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);

            var webKitSettings = new WebKitConverterSettings

            {

                AspectRatio = AspectRatio.FitPageSize,

                PdfPageSize = PdfPageSize.A4,

                Orientation = PdfPageOrientation.Landscape,

                SinglePageLayout = SinglePageLayout.FitWidth

            };

            htmlConverter.ConverterSettings = webKitSettings;



            var stream = new MemoryStream();


            using (var doc = new PdfDocument())

            {

                PdfPage page = doc.Pages.Add();

                PdfPage currentPage = doc.Pages[0];


                PdfDocument document = htmlConverter.Convert(htmlString, string.Empty);

                PdfPage pdfPage = document.Pages[0];

                PdfTemplate template = pdfPage.CreateTemplate();


                currentPage.Graphics.DrawPdfTemplate(template, new PointF(50,50));


                // Save the document to stream

                doc.Save(stream);


                // Save the stream as pdf

                FileStream pdfFileStream = File.OpenWrite("TestReport.pdf");

                stream.WriteTo(pdfFileStream);


                // Save the stream as image (png)

                using (var loadedDoc = new PdfLoadedDocument(stream))

                {

                    using (var imageStream = new MemoryStream())

                    {

                        Image image = loadedDoc.ExportAsImage(0);

                        image.Save(imageStream, ImageFormat.Png);

                        imageStream.Position = 0;

                        FileStream outStream = File.OpenWrite("TestReport.png");

                        imageStream.WriteTo(outStream);

                        outStream.Flush();

                        imageStream.Dispose();

                    }

                }

            }


3 Replies 1 reply marked as answer

GK Gowthamraj Kumar Syncfusion Team October 7, 2021 09:59 AM UTC

Hi Nurul, 

Thank you for contacting Syncfusion support. 

PDF to Image  
We can export the Pdf document into image with good quality by using dpi values. Please try the below code snippet on your end and let us know the result. 
 
//Export as good quality image   
Image image = loadedDoc.ExportAsImage(0, 300, 300); 
image.Save(imageStream, ImageFormat.Png); 
 
Please find the output document for your reference,  
 
Html to Image 
The WebKit HTML Converter provides support for converting HTML string to Image. Please refer the below documentation link,
https://help.syncfusion.com/file-formats/pdf/working-with-document-conversions#html-string-to-raster-image
 
 
//Convert HTML string to Image 
Image[] HtmltoImage = htmlConverter.ConvertToImage(htmlString, string.Empty); 
 
//Save the image                HtmltoImage[0].Save("HtmlToImage.png"); 
 
Please let us know if you need any further assistance with this. 
 

Regards, 
Gowthamraj K 


Marked as answer

NH Nurul Hasan replied to Gowthamraj Kumar October 13, 2021 07:48 AM UTC

It's working. Thank You



GK Gowthamraj Kumar Syncfusion Team October 13, 2021 09:11 AM UTC

Hi Nurul, 

Thank you for your update. We are glad to know that your problem has been solved. 

Please let us know if you need any further assistance with this. 

Regards, 
Gowthamraj K 


Loader.
Up arrow icon