ASP.NET Code Book

A directory of ASP.NET tutorials,ASP.NET Codebook, applications, scripts, assemblies and articles for the novice to professional developer

Blog Archive

Your Ad Here

Monday, May 4, 2009

How To Export WebPage as Image in asp..net




In this post i will show how to export Webpage as image in asp.net

  1. Start Visual Studio 2005.
  2. Create a Web site.
  3. Add Reference of System.Windows.Forms
  4. Add AspCompat="true" in page attribute.
  5. Verify that the following namespaces are included in the ExportWebPageToImage.c


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;



Here is complete source code




<%@ Page Language="C#" AspCompat="true" AutoEventWireup="true" CodeFile="ExportWebPageToImage.aspx.cs"
Inherits="ExportWebPageToImage" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtUrl" runat="server" Width="208px"></asp:TextBox><br />
<asp:Button ID="btnConvert" runat="server" Text="Convert Page To Image" OnClick="btnConvert_Click" />
</div>
</form>
</body>
</html>





using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

public partial class ExportWebPageToImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


}

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
}
protected void btnConvert_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();
}
}

Monday, April 20, 2009

How To Read xml using JQuery


In this post i will show how to read xml using JQuery





<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>

<script type="text/javascript">

$(document).ready(function() {
$.get('Data.xml', function(d) {
var data = "";
var startTag = "<table border='1' id='mainTable'><tbody><tr><td style=\"width: 120px\">Name</td><td style=\"width: 120px\">Link</td></tr>";
var endTag = "</tbody></table>";
$(d).find('url').each(function() {
var $url = $(this);
var link = $url.find('link').text();
var name = $url.find('name').text();
data += '<tr><td>' + name + '</td>';
data += '<td>' + link + '</td></tr>';
})
$("#content").html(startTag + data + endTag);
;
});

});
</script>

</head>
<body>
<div id="content">
</div>
</body>
</html>


Data.xml

<?xml version="1.0" encoding="utf-8" ?>
<Urls>
<url>
<name>google</name>
<link>
www.google.com
</link>
</url>
<url>
<name>aspdotnetcodebook</name>
<link>http://aspdotnetcodebook.blogspot.com</link>
</url>
</Urls>

Sunday, April 19, 2009

jQuery Ajax call and result XML parsing in asp.net





In this post i will show how to use Jquery in asp.net

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog box, click Visual C# Projects
  4. Add a new xml file and copy the following xml.

labels.xml
<?xml version="1.0" encoding="utf-8"?>
<labels>
<label id="1">
<name>Test</name>
<address>
<street>street1</street>
<city>City1</city>
<province>ID</province>
</address>
</label>
<label id='2'>
<name>Name2</name>
<address>
<street>Street2</street>
<city>City2</city>
<province>CT</province>
</address>
</label>

</labels>

5. Add a reference to the jQuery script
6.Copy below code in aspx page.




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>

<script type="text/javascript">
$(function() {
$('#update-target a').click(function() {
$.ajax({
type: "GET",
url: "labels.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('label').each(function(){
var id_text = $(this).attr('id')
var name_text = $(this).find('name').text()

$('<li></li>')
.html(name_text + ' (' + id_text + ')')
.appendTo('#update-target ol');
}); //close each(
}
}); //close $.ajax(
}); //close click(
}); //close $(
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<div id='update-target'>
<a href="#">Click here to load addresses</a>
<ol>
</ol>
</div>
</p>
</div>
</form>
</body>
</html>

Your Ad Here

SQL SERVER

One .Net Way

Over the years I have collected some best asp.net code while googling........I hope this information is as useful to you as it has been for me("If you find my post contains a reference to a third party World Wide Web site, I am providing this information as a convenience to you.I cannot make any representations regarding the quality,safety, or suitability of any software or information found there")