3

I want to Generate Image (JPEG) from HTML Code Entered in Textarea or other Input Controls (Not Whole Web page) using C# in Asp.Net MVC .MVC doesn't Support WebBrowser Control.

This HTML contains both data and image

HTML

<b>Convert Html To Image<b> 
<br/>
<img src="C:\Logo.jpg" alt="Smiley face" width="42" height="42"> 

I passed the html code to the Controller for the Bitmap Image Creating Function

private Bitmap CreateBitmapImage(string html)
{
   Bitmap objBmpImage = new Bitmap(1, 1);

   int intWidth = 0;
   int intHeight = 0;

   // Create the Font object for the image text drawing.
   Font objFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold,  System.Drawing.GraphicsUnit.Pixel);

   // Create a graphics object to measure the text's width and height.
   Graphics objGraphics = Graphics.FromImage(objBmpImage);

   // This is where the bitmap size is determined.
   intWidth = (int)objGraphics.MeasureString(html, objFont).Width;
   intHeight = (int)objGraphics.MeasureString(html, objFont).Height;

   // Create the bmpImage again with the correct size for the text and font.
   objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));

   // Add the colors to the new bitmap.
   objGraphics = Graphics.FromImage(objBmpImage);

   // Set Background color
   objGraphics.Clear(Color.White);
   objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
   objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
   objGraphics.DrawString(html, objFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
   objGraphics.Flush();
   return (objBmpImage);
}

But it return the Output Like this

enter image description here

But Required OutPut

enter image description here

Is there any way to Generate this(Stepes to Generate Image from html) or any third party Opensource Component Available like HtmlToImageConverter ?

snowp
  • 495
  • 1
  • 6
  • 22
  • 2
    http://stackoverflow.com/questions/7803201/capturing-webpage-as-image-in-c-ensuring-javascript-rendered-elements-are-visi or http://awesomium.com/ – I4V May 08 '13 at 07:19
  • @ I4V thank u for ur reply i don't want to use third party .net component and IECapt doesn't support all browser – snowp May 08 '13 at 07:39
  • @Prabu only one "browser" needs to be supported, right? Or do you want to generate several JPEGs, one for each browser emulator? – C.Evenhuis May 08 '13 at 07:52
  • 1
    @Prabu: I'm curious as to what you think the DrawString method actually does? The method name itself says that it draws a string. No where does it imply that it would parse a string as html, download any images in the html, and then render it all out like a webpage. – rossisdead May 08 '13 at 14:37
  • Another option may be to convert to pdf using itextsharp – Tassadaque Aug 05 '13 at 10:20

2 Answers2

3

It is possible to render HTML to image with HtmlRenderer dll. You can get it in HtmlRenderer.WinForms NuGet package (it'll also install HtmlRenderer.Core)

There is a list of issues about it, but it works. You can do something like

public static Image RenderToImage<T>(String fullViewName, T model)
{
    String html = RenderViewToString(fullViewName, model);
    var image = HtmlRender.RenderToImage(html);
    return image;
}

public static String RenderViewToString<T>(String fullViewName, T model)
{
    String viewText = File.ReadAllText(fullViewName);
    String renderedText = Razor.Parse(viewText, model); // RazorEngine here. I don't have MVC
    return renderedText;
}
Oleg
  • 1,291
  • 13
  • 16
0

webSupergoo generate jpg from html. It is not open source but free to download You may also convert html to pdf if it may solve your problem using itextsharp

Tassadaque
  • 8,129
  • 13
  • 57
  • 89
  • thank u for ur respone but its not solved my pbm i need it for Our Business Card Design website..(user Customize the layout as per requirement then they save it(saved image Url store in database)) – snowp Aug 10 '13 at 10:35