To Create multiple folders and create screenshots of URL's and save those screenshots into newly created folders.
1. Create a Windows application and add a button to it.
2. Extra Namespaces that we will require are:
using System.Data.Sql;
using System.Data.SqlClient;
using System.Drawing.Imaging;
using System.Collections;
using System.Text.RegularExpressions;
using System.Configuration;
using System.IO;3. For browsing URL we can use WebBrowser control of ASP.NET. public partial class Form1: Form
{
WebBrowser wb = new System.Windows.Forms.WebBrowser();
public Form1()
{
wb.Height = Math.Max(1024, Screen.PrimaryScreen.Bounds.Height);
wb.Width = Math.Max(1024, Screen.PrimaryScreen.Bounds.Width);
InitializeComponent();
this.wb.DocumentCompleted += newWebBrowserDocumentCompletedEventHandler(this.OnDocumentCompleted);
}
}4. On button's click event write following code:private void button1_Click(object sender, EventArgs e)
{
List < string > POVendor = new List < string > ();
CreateFolder cfldr = new CreateFolder();
List < KeyValuePair < string, string >> list = new List < KeyValuePair < string, string >> ();
SqlCommand command = new SqlCommand("SQLCOMMAND", cnn);
SqlDataReader productsSqlDataReader = command.ExecuteReader();
int productNameColPos = productsSqlDataReader.GetOrdinal("VendorName");
while (productsSqlDataReader.Read())
{
SupplierName = productsSqlDataReader.GetString(productNameColPos);
POVendor.Add(SupplierName);
}
productsSqlDataReader.Close();
foreach(string vendorName in POVendor)
{
//Remove the special characters from name returned from database as we have to create folder with that name
Regex r = new Regex(@"[~`!@#$%^&*()+=|\{}':;.,<>/?[\]"
"_-]");
if (r.IsMatch(vendorName)) NavigateVendorName = RemoveSpeciaCharecters.RemoveSpecialCharacters(vendorName);
else if (vendorName == "" || vendorName == "*") NavigateVendorName = "Default";
else NavigateVendorName = vendorName;
try
{
cfldr.VendorFolderCreate(NavigateVendorName);
string webAddressString;
webAddressString = "http://netclassic/STAR/Summary.asp?PO=" + POUrl; // URL to be browsed for this Folder
string screenshotName = cfldr.POscreenshotName(NavigateVendorName, POUrl); //Return path in which screenshot will be saved
list.Add(new KeyValuePair < string, string > (webAddressString, screenshotName));
}
catch
{
//LogError
folderCreated = false;
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\vendorlog.txt", true);
file.WriteLine(vendorName);
file.Close();
}
}
NavigateToNextUrl();
}
private void NavigateToNextUrl()
{
if (urlIndex < list.Count)
{
string webAddressString;
webAddressString = list[urlIndex].Key;
Uri webAddress;
if (Uri.TryCreate(webAddressString, UriKind.Absolute, out webAddress)) this.wb.Navigate(webAddress);
else MessageBox.Show("Done ! !");
}
//For capturing screenshot use ONDocumentComplete event of web browser
private void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
try
{
if (this.wb.ReadyState == WebBrowserReadyState.Complete && e.Url == this.wb.Url)
{
wb.AllowNavigation = true;
// Debug.Write(this.WebBrowser.Document);
this.SaveScreenshot(list[urlIndex].Value);
urlIndex++;
NavigateToNextUrl();
}
}
catch (Exception ex)
{
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\log.txt", true);
file.WriteLine(list[urlIndex].Key);
file.Close();
urlIndex++;
NavigateToNextUrl();
}
}
private void SaveScreenshot(string fileName)
{
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
int wbwidth = wb.Document.Body.ScrollRectangle.Width;
int wbheight = wb.Document.Body.ScrollRectangle.Height;
using(Graphics graphics = this.wb.CreateGraphics())
using(Bitmap bitmap = new Bitmap(wbwidth, wbheight, graphics))
{
Rectangle bounds = new Rectangle(0, 0, wbwidth, wbheight);
this.wb.DrawToBitmap(bitmap, bounds);
bitmap.Save(fileName, ImageFormat.Png);
}
}
} I used it to create more than 4 lakh folders and there screenshots programmatically.