PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Embedded images in HTML being enlarged
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3916
Page 1 of 1

Author:  kej [ Tue Feb 26, 2019 1:38 pm ]
Post subject:  Embedded images in HTML being enlarged

Hi,
I'm using PDFsharp along with the HTML Renderer (https://theartofdev.com/html-renderer/) library to save an MVC view to a PDF. The HTML has IMG tags with the image content inline as base 64 strings. The resulting PDF has the images present. However, they have been increased in size 20-30% and are blurry. I've been able to add CSS to the page to force the maximum size of the IMG to prevent scaling up too much. But the image is still blurry.

I'm trying to figure out if there is anything I can do with PDFsharp to prevent this behavior. I'm not sure if the problem is there or in the HTML Renderer. I see that there is an event handler available to manually handle image processing but have yet to get that to work.

Any help is appreciated.

Author:  Thomas Hoevel [ Tue Feb 26, 2019 2:51 pm ]
Post subject:  Re: Embedded images in HTML being enlarged

Hi!

To prevent blurriness set "image.Interpolate = false;" for the image.
If you cannot do that in the image processing callback, then you'll have to modify the HtmlRenderer code.

Author:  kej [ Tue Feb 26, 2019 4:03 pm ]
Post subject:  Re: Embedded images in HTML being enlarged

Thanks. I've got the HTML Renderer callback work now but still no luck on the image quality. Here's what I've tried:

byte[] imageData = Convert.FromBase64String(eventArgs.Src.Substring(5));
using (var ms = new MemoryStream(imageData))
{
XImage ximg = XImage.FromStream(ms);
ximg.Interpolate = false;

eventArgs.Handled = true;
eventArgs.Callback(ximg);
}

The XImage passed to callback is then supplied to PDFsharp as the image. Any ideas?

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/