Image loading issue

Sep 30, 2013 at 3:49 AM
I am using the below code to convet an html to image. I ahev an image whcih is placed as the background of a div and then writing a text, "Test" over that. Then I am converting teh same html to a png image. Below is teh code for that.

The issue is, for the first time the image is coming properly. Next time when I try to run teh application, the original image which is used as the div background is locked. I tried deleting the file from folder manually, Iths throwing error that the fil is locked in IIS worker process.

Please help.

Bitmap m_Bitmap = new Bitmap(1000, 682);
PointF point = new PointF(0, 0);
string path =" D://Images//OriginalImage.png";
string newPath = "D://Images//NewImage.png";
SizeF maxSize = new System.Drawing.SizeF(674, 690);

string html = "<html><body><div style=\"background-image:url(" + path + ");height:674px;width:670px;text-align:center;\"><div style=\"height:674px;width:670px;margin-top:400px;\"><span>Test</span></div></body></html>";

HtmlRenderer.HtmlRender.Render(Graphics.FromImage(m_Bitmap), html, point, maxSize);
m_Bitmap.Save(newPath, ImageFormat.Png);
Developer
Oct 4, 2013 at 10:27 AM
Sorry, stupid bug.
In HtmlRender replace the last method with:
public static SizeF Render(Graphics g, string html, PointF location, SizeF maxSize, CssData cssData, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad, EventHandler<HtmlImageLoadEventArgs> imageLoad)
{
    ArgChecker.AssertArgNotNull(g, "g");

    SizeF actualSize = SizeF.Empty;
    if (!string.IsNullOrEmpty(html))
    {
        Region prevClip = null;
        if (maxSize.Height > 0)
        {
            prevClip = g.Clip;
            g.SetClip(new RectangleF(location, maxSize));
        }

        using(var container = new HtmlContainer())
        {
            if (stylesheetLoad != null)
                container.StylesheetLoad += stylesheetLoad;
            if (imageLoad != null)
                container.ImageLoad += imageLoad;
            container.SetHtml(html, cssData);
            container.Location = location;
            container.MaxSize = maxSize;
            container.PerformLayout(g);
            container.PerformPaint(g);
            if (stylesheetLoad != null)
                container.StylesheetLoad -= stylesheetLoad;
            if (imageLoad != null)
                container.ImageLoad -= imageLoad;
                
            if (prevClip != null)
            {
                g.SetClip(prevClip, CombineMode.Replace);
            }

            actualSize = container.ActualSize;
        }
    }

    return actualSize;
}
Dec 4, 2013 at 6:02 PM
Edited Dec 4, 2013 at 6:06 PM
It appears that this bug also affects RenderToImage. I've tried making the following modification but it does not seem to fix the problem:

// Existing line within RenderToImage
htmlContainer.PerformPaint(memoryGraphics);

// New code under PerformPaint which doesn't fix the problem
if (stylesheetLoad != null)
htmlContainer.StylesheetLoad -= stylesheetLoad;
if (imageLoad != null)
htmlContainer.ImageLoad -= imageLoad;

Any thoughts?

Thanks...definitely hoping to use this library.

EDIT: Your fix did indeed resolve the issue with HtmlRender.Render, so I'm using that instead (for now)...thanks!

Billy
Dec 4, 2013 at 6:10 PM
Doh...can't use HtmlRender.Render because of the pixelation problem discussed at https://htmlrenderer.codeplex.com/discussions/468725 (which I just discovered).

Please let me know if you're able to provide a fix for HtmlRender.RenderToImage.

Thanks again,
Billy
Developer
Dec 5, 2013 at 9:29 AM
Edited Dec 5, 2013 at 9:57 AM
I run this code with the latest (1.4.10.0) and it works fine... can you verify the problem, do you need to create image with transparent background?
string path = " D://OriginalImage.png";
string newPath = "D://NewImage.png";
Size maxSize = new Size(674, 690);
string html = "<html><body><div style=\"background-image:url(" + path + ");height:674px;width:670px;text-align:center;\"><div style=\"height:674px;width:670px;margin-top:400px;\"><span>Test</span></div></body></html>";
var img = HtmlRenderer.HtmlRender.RenderToImage(html, maxSize);
img.Save(newPath, ImageFormat.Png);
Result:
Image