Click here to Skip to main content
16,020,673 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
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:

C#
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);

img.Dispose();
ms.Close();




Thanks for any help
Posted
Updated 19-Dec-11 19:07pm
v2
Comments
Sergey Alexandrovich Kryukov 20-Dec-11 2:07am    
Gosh, why?!
--SA
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.

This link will help you better.

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

thanks
 
Share this answer
 
C#
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 : http://www.sitepoint.com/generating-asp-net-images-fly/
 
Share this answer
 
v2
Comments
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?
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?)
bbirajdar 23-Dec-11 4:01am    
I guess this might work for your scenario

http://www.acasystems.com/en/web-thumb-activex/faq-convert-html-to-image-in-c-sharp.htm . Its paid software
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.

http://www.converthtmltoimage.com/Convert-html-to-png-in-c-sharp-net.html[^]

C#
WebsitesScreenshot.WebsitesScreenshot _Obj;
_Obj = new WebsitesScreenshot.WebsitesScreenshot();
WebsitesScreenshot.WebsitesScreenshot.Result _Result;            
_Result = _Obj.CaptureWebpage("http://www.msn.com");
if (_Result == WebsitesScreenshot.
			WebsitesScreenshot.Result.Captured)
{
	_Obj.ImageWidth = 200;
	_Obj.ImageHeight = 300;
	_Obj.ImageFormat = WebsitesScreenshot.
		WebsitesScreenshot.ImageFormats.PNG;
	_Obj.SaveImage("c:\\msn.png");
} 
_Obj.Dispose();
 
Share this answer
 

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