Click here to Skip to main content
13,046,375 members (69,347 online)
Rate this:
Please Sign up or sign in to vote.
Hi all,

I have a situation where I need to save the content in HTML div as Image in web server.

I tried to use the Graphic classes, but those could convert the Text part into Image. If I have any image inside that HTML div or any other object then I get Invalid conversion error.

I would like to find out if there's any other way to do it?

Following is the code I am using:

System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url);
System.Net.WebResponse webResponse = webRequest.GetResponse() ;
System.IO.StreamReader sr = new System.IO.StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
content = sr.ReadToEnd();
//save to file
byte[] b = StreamFile(content );

System.IO.MemoryStream ms = new System.IO.MemoryStream(b);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms,false,true);
img.Save(@"D:\pic.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

Thanks for any help
Posted 19-Dec-11 19:05pm
Updated 19-Dec-11 19:07pm
SAKryukov 20-Dec-11 2:07am
Gosh, why?!
Ashish Dev1 20-Dec-11 2:24am
I am working on an item customization application on which User selects different colors for different items. Then I need to save an image for his selection made.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

protected void Page_Load(object sender, EventArgs e)
 if (IsPostBack)
 //create a new Bitmap objec
 Bitmap oBitmap = new Bitmap(468, 60);
 //create a new Graphics object, which will allow us to draw on our bitmap:
 Graphics oGraphic = Graphics.FromImage(oBitmap);
 //define the background color of our dynamic image:
 String scolor = BackgroundColor.SelectedValue;
 //get the values that were posted back
 String sText = Text.Text;
 String sFont = Font.Text;
 Color oColor = Color.FromName(BackgroundColor.SelectedValue);
 // create two brushes, which will help us to draw our image.
 //The first one, oBrush, will be used to draw the background of the image:
 // second brush will be used to draw the text entered by the user
 SolidBrush oBrush = new SolidBrush(oColor);
 SolidBrush oBrushwrite = new SolidBrush(Color.White);
 //fill the image rectangle,
 oGraphic.FillRectangle(oBrush, 0, 0, 468, 60);
 //user chose a font for the text, so we pass sFont as the first parameter, and hardcode the size:
 Font oFont = new Font(sFont, 13);
 // X and Y point of text
 PointF oPoint = new PointF(5f,5f);
 //draw our text:
 oGraphic.DrawString(sText, oFont, oBrushwrite, oPoint);
 //send image directly to the browser
 Response.ContentType = "image/jpeg";
 oBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);

Credit goes to :
Ashish Dev1 23-Dec-11 1:57am
Hi B Birajdar,

Thanks for this info. Actually I have checked this one already. But my situation is, I have a Html div and within it I have some images and text, now I want to convert that div's Inner Html into an image. can this is possible without using any paid third party tool?

Any suggestion?
B Birajdar 23-Dec-11 4:01am
I guess this might work for your scenario . Its paid software
lucosta 23-Nov-12 14:15pm
I have the same problem, one div with an image and one lable and a textbox, that I must transform it into a JPG or a PNG.

But insted of showing directly in browser I need to upload/insert into a SQL Table.
Is that possible? And how (what kind of data type I will need?)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I found this .NET SDK to convert HTML to Image in .net
few lines of code require to convert url to image.
download this library and try the following code.[^]

WebsitesScreenshot.WebsitesScreenshot _Obj;
_Obj = new WebsitesScreenshot.WebsitesScreenshot();
WebsitesScreenshot.WebsitesScreenshot.Result _Result;            
_Result = _Obj.CaptureWebpage("");
if (_Result == WebsitesScreenshot.
	_Obj.ImageWidth = 200;
	_Obj.ImageHeight = 300;
	_Obj.ImageFormat = WebsitesScreenshot.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

This link will help you better.

Get ASP.NET C# 2.0 Website Thumbnail Screenshot[^]


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 1 Jul 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100