Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a requirement where I want to convert a whole view which contains HTML & CSS elements to image and then pass it to the users. I am just wondering is there any builtin library in C# to generate images out of a view on the fly?
Please do not suggest me third party tools.

What I have tried:

I am lost and I don't know where to start from.
Posted
Updated 19-Jan-17 6:33am

The only built-in option would be to use the WebBrowser control in a new thread, as described in this StackOverflow answer[^]. However, that's probably not going to work very well in an ASP.NET site.

Why are you so opposed to third-party tools? For example, HTML-Renderer[^] looks like it would do exactly what you need, and is free to use. The license[^] looks pretty relaxed too.
 
Share this answer
 
Comments
Barcelonista Naser 19-Jan-17 12:40pm    
How can i configure it to work with asp.net mvc? Is it working with asp.net mvc?
Barcelonista Naser 19-Jan-17 12:42pm    
There is no example on asp.net mvc
Richard Deeming 19-Jan-17 12:46pm    
This example[^] shows you how to generate an image from HTML.

This blog post[^] shows you how to render an MVC view to a string.
Barcelonista Naser 19-Jan-17 13:22pm    
I can't resolve HtmlRender although I installed the HTML renderer core with nuget. what is the problem?
Richard Deeming 19-Jan-17 13:40pm    
Looks like you need to add either the HtmlRenderer.WinForms or HtmlRenderer.WPF package to access that class.
You could just take a screen capture and adjust the image size accordingly? I found this solution on Google with little searching.

C#
Rectangle bounds = Screen.GetBounds(Point.Empty);
using(Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
    using(Graphics g = Graphics.FromImage(bitmap))
    {
         g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
    }
    bitmap.Save("test.jpg", ImageFormat.Jpeg);
}
 
Share this answer
 
Comments
Barcelonista Naser 19-Jan-17 12:39pm    
I don't want to render the view to the user I just simply want to generate an image out of html that never will be rendered.
eddieangel 19-Jan-17 13:49pm    
Ok. Your options are definitely limited at that point. Once you stop relying on the browser to render your HTML you have to find another engine. As Richard said above, if you don't want to rely on a third party control you are going to have to reinvent the wheel and have it render in your code and then convert to a bmp.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900