Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp C# Save Web Page URL To Image

C# Save Web Page URL To Image

Simple C# code below will capture a particular web page via its URL and save it to an image. It can work with long scrolling web page that includes images, JavaScript and even with Flash. That means a fully web page will be captured and saved to an image file.

Note that we will get a large image if the web page we’re capturing is too long (small scroll bar) and the generated image is very similar with what we see when explore this web page in Internet Explorer

I know that there are some tools and services that allow you to do that but if you’re working on .Net form application, the C# code below may become your own solution.

C# Save Web Page To Image

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            string url = "http://4rapiddev.com/facebook-graph-api/php-load-facebook-albums-and-save-to-mysql-database/";
 
            Bitmap thumbnail = SaveWebPage2Image(url);
 
            //specify where image will be save
            thumbnail.Save("d:/web-shot.png", System.Drawing.Imaging.ImageFormat.Png);
        }
 
        public Bitmap SaveWebPage2Image(string url)
        {
            // Load the webpage into a WebBrowser control
            WebBrowser wb = new WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(url);
 
            while (wb.ReadyState != WebBrowserReadyState.Complete) { System.Windows.Forms.Application.DoEvents(); }
 
            // Take Screenshot of the web pages full width
            wb.Width = wb.Document.Body.ScrollRectangle.Width;
 
            // Take Screenshot of the web pages full height
            wb.Height = wb.Document.Body.ScrollRectangle.Height;
 
            // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
            Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
            wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
            wb.Dispose();
 
            return bitmap;
        }
    }
}

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string url = "http://4rapiddev.com/facebook-graph-api/php-load-facebook-albums-and-save-to-mysql-database/"; Bitmap thumbnail = SaveWebPage2Image(url); //specify where image will be save thumbnail.Save("d:/web-shot.png", System.Drawing.Imaging.ImageFormat.Png); } public Bitmap SaveWebPage2Image(string url) { // Load the webpage into a WebBrowser control WebBrowser wb = new WebBrowser(); wb.ScrollBarsEnabled = false; wb.ScriptErrorsSuppressed = true; wb.Navigate(url); while (wb.ReadyState != WebBrowserReadyState.Complete) { System.Windows.Forms.Application.DoEvents(); } // Take Screenshot of the web pages full width wb.Width = wb.Document.Body.ScrollRectangle.Width; // Take Screenshot of the web pages full height wb.Height = wb.Document.Body.ScrollRectangle.Height; // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control Bitmap bitmap = new Bitmap(wb.Width, wb.Height); wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height)); wb.Dispose(); return bitmap; } } }

Example:

web-shot

web-shot

Few notes here:

  • 1. Using System.Drawing namespace
  • 2. Replace URL with URL of web page you want to save
  • 3. Specify where you want to save the image
Dec 2, 2011Hoan Huynh
PHP Load Facebook Albums And Save To MySQL DatabaseC# Get File Extension Without Sub String Or Split Function
You Might Also Like:
  • C# Generate Website Screenshot And Save Thumbnail
  • ASP.Net C# Download Or Save Image File From URL
  • ASP.NET Resize Image High Quality
  • Get Image Width Height With JQuery And JavaScript
  • WordPress View Large Image Without Leaving Current Page
  • Share Page On Facebook With Thumbnail Featured Image
  • PHP Get Image Width And Height
  • JavaScript Display Leaving Confirm Box Leave This Page Or Stay On This Page
  • How To Ask People To Like Your Facebook Page On The Landing Page
  • Save Video File With Custom Name In CamStudio Recorder
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

9 years ago CSharpBitmap, DrawToBitmap, WebBrowser, WebBrowserReadyState1,541
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,237 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,087 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,872 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,343 views
C# Read Json From URL And Parse/Deserialize Json
9,850 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Term Papers – Easy to Take Care of
  • How to Write Term Papers in Online Tutorials
  • How to Plan Writing an Essay
  • How to Apply For a Payday Loan With Bad Credit
  • Statistics For Sale – How To Compose One
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (123)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development