Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 ASP.NET graphics
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);
 
img.Dispose();
ms.Close();
 

 
Thanks for any help
Posted 19-Dec-11 20:05pm
Edited 19-Dec-11 20:07pm
v2
Comments
SAKryukov at 20-Dec-11 2:07am
   
Gosh, why?!
--SA
Ashish Dev1 at 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
good
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 : http://www.sitepoint.com/generating-asp-net-images-fly/
  Permalink  
v2
Comments
Ashish Dev1 at 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 at 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?)
B Birajdar at 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
Rate this: bad
good
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.
 
http://www.converthtmltoimage.com/Convert-html-to-png-in-c-sharp-net.html[^]
 
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();
  Permalink  
Rate this: bad
good
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[^]
 
thanks
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 DamithSL 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 1 Jul 2014
Copyright © CodeProject, 1999-2014
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