<div id="contentsDiv"> <b>Write some thing</b> <br /> <img alt="Koala" src="Images/Koala.jpg" width="400" height="400" align="right"/> <br< /> <i>Some thing means any thing </i> </div>
Have the above div tag contents in a seprate page and make sure you have some URL which you can load that page. Now you can use the below code and pass that URL as a parameter to below method so it will generate the image from the HTML.
Note: This is using Windows Forms.
public System.Drawing.Bitmap CaptureWebPage(string URL)
{
// create a hidden web browser, which will navigate to the page
System.Windows.Forms.WebBrowser web = new System.Windows.Forms.WebBrowser();
// we don't want scrollbars on our image
web.ScrollBarsEnabled = false;
// don't let any errors shine through
web.ScriptErrorsSuppressed = true;
// let's load up that page!
web.Navigate(URL);
// wait until the page is fully loaded
while (web.ReadyState != WebBrowserReadyState.Complete)
System.Windows.Forms.Application.DoEvents();
System.Threading.Thread.Sleep(1500); // allow time for page scripts to update
// the appearance of the page
// set the size of our web browser to be the same size as the page
int width = web.Document.Body.ScrollRectangle.Width;
int height = web.Document.Body.ScrollRectangle.Height;
web.Width = width;
web.Height = height;
// a bitmap that we will draw to
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height);
// draw the web browser to the bitmap
web.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height));
return bmp; // return the bitmap for processing
}
So your button code behind should looks like below.
jitteshh
Member
40 Points
63 Posts
About Div Tag convert into image in c#
Feb 02, 2012 03:51 AM|LINK
Hi,
I am a Software Trainee in asp.net.
This is my div tag.
<div id="contentsDiv"> <b>Write some thing</b> <br /> <img alt="Koala" src="Images/Koala.jpg" width="400" height="400" align="right"/> <br< /> <i>Some thing means any thing </i> </div> <asp:Button ID="Export" runat="server" Text="Export" onclick="Export_Click" />Now,On click of Export button ,I want to convert the contents of <div>tag into an image and save that image in particular folder.Please help me and thanks in advance.
subhash.shel...
Contributor
2137 Points
487 Posts
Re: About Div Tag convert into image in c#
Feb 02, 2012 04:05 AM|LINK
Hi,
you can refer below link...
http://x10hosting.com/forums/programming-help/71078-convert-div-into-image.html
http://www.dynamicdrive.com/forums/showthread.php?t=31137
Subhash
Please, Mark as Answer if this reply helped you.
salman beher...
All-Star
30563 Points
5844 Posts
Re: About Div Tag convert into image in c#
Feb 02, 2012 04:10 AM|LINK
Hi,
same discuession::
http://forums.asp.net/t/1117630.aspx
http://forums.asp.net/t/1545780.aspx/1
Sincerely,
Salman
jitteshh
Member
40 Points
63 Posts
Re: About Div Tag convert into image in c#
Feb 02, 2012 05:06 AM|LINK
Hi Salman behera ,
Thanks for the link ,but still I didn't get the exact solution .
Kindly help
jitteshh
Member
40 Points
63 Posts
Re: About Div Tag convert into image in c#
Feb 02, 2012 05:07 AM|LINK
Hi Subash Sheladia ,
Thanks for the link ,but still I didn't get the exact solution .
Kindly help
srinanthuram
Contributor
6800 Points
1549 Posts
Re: About Div Tag convert into image in c#
Feb 02, 2012 05:31 AM|LINK
hi
see this url
http://stackoverflow.com/questions/1972739/convert-a-html-control-div-or-table-to-an-image-using-c-sharp
thank u
salman beher...
All-Star
30563 Points
5844 Posts
Re: About Div Tag convert into image in c#
Feb 02, 2012 05:34 AM|LINK
let me know where you stuff??
Sincerely,
Salman
jitteshh
Member
40 Points
63 Posts
Re: About Div Tag convert into image in c#
Feb 02, 2012 05:41 AM|LINK
Hi Srinanthuram,
Thanks for the links ,but here also I didn't get the solution .
Please suggest me the source code for my problem.
kindly help.
Ruchira
All-Star
42926 Points
7023 Posts
MVP
Re: About Div Tag convert into image in c#
Feb 02, 2012 06:06 AM|LINK
Have the above div tag contents in a seprate page and make sure you have some URL which you can load that page. Now you can use the below code and pass that URL as a parameter to below method so it will generate the image from the HTML.
Note: This is using Windows Forms.
public System.Drawing.Bitmap CaptureWebPage(string URL) { // create a hidden web browser, which will navigate to the page System.Windows.Forms.WebBrowser web = new System.Windows.Forms.WebBrowser(); // we don't want scrollbars on our image web.ScrollBarsEnabled = false; // don't let any errors shine through web.ScriptErrorsSuppressed = true; // let's load up that page! web.Navigate(URL); // wait until the page is fully loaded while (web.ReadyState != WebBrowserReadyState.Complete) System.Windows.Forms.Application.DoEvents(); System.Threading.Thread.Sleep(1500); // allow time for page scripts to update // the appearance of the page // set the size of our web browser to be the same size as the page int width = web.Document.Body.ScrollRectangle.Width; int height = web.Document.Body.ScrollRectangle.Height; web.Width = width; web.Height = height; // a bitmap that we will draw to System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height); // draw the web browser to the bitmap web.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height)); return bmp; // return the bitmap for processing }So your button code behind should looks like below.
protected void Export_Click(object sender, EventArgs e) { Bitmap bitmap = new Bitmap(CaptureWebPage(txtUrl.Text)); Response.ContentType = "image/jpeg"; bitmap.Save(Response.OutputStream, ImageFormat.Jpeg); bitmap.Dispose(); bitmap.Dispose(); Response.End(); }You may have to import following namespaces.
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.srinanthuram
Contributor
6800 Points
1549 Posts
Re: About Div Tag convert into image in c#
Feb 02, 2012 07:24 AM|LINK
hi
get imgid.then save u r folder Map Paththank u