Пишу сервис со скриншотами с веб-страниц локальных сайтов.Я использую элемент управления WebBrowser для захвата веб-страницы в качестве изображения.Ниже код, который я использую:
namespace MakeScreenShotURL
{
class Program
{
private static Config config = new Config(System.Reflection.Assembly.GetExecutingAssembly().Location);
[STAThread]
static void Main()
{
int width = 1700;
int height = 1700;
string username = "userdata";
string password = "passworddata";
int currentmonth = DateTime.Now.Month;
int nextmonth = DateTime.Now.Month + 1;
int year = DateTime.Now.Year;
screen(width, height, year, nextmonth, currentmonth, username, password);
}
static void screen(int width, int height, int year, int nextmonth, int currentmonth, string username, string password)
{
string PlaceCode = config.GetAppSetting("place");
string[] Places = PlaceCode.Split(';'); //contains a list of sites for example: Berlin; Munich; Dresden
foreach (string pl in Places)
{
using (WebBrowser browser = new WebBrowser())
{
browser.Width = width;
browser.Height = height;
browser.ScrollBarsEnabled = true;
Uri uri = new Uri(" http://localsite.php?y=" + year + "&m=" + nextmonth + "&p=" + pl + " ");
string additionalHeaderInfo = "Authorization: Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password)) + System.Environment.NewLine;
browser.Navigate(uri, null, null, additionalHeaderInfo);
Wait(2); //waiting for pages to load 2 seconds
using (Graphics graphics = browser.CreateGraphics())
using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height, graphics))
{
Rectangle bounds = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
browser.DrawToBitmap(bitmap, bounds);
bitmap.Save("C:/Users/user/image/screenshot" + pl + ".jpeg", ImageFormat.Jpeg);
}
Application.Run();
}
}
Application.Exit();
}
static void Wait(int number)
{
DateTime time = DateTime.Now;
do
{
Application.DoEvents();
}
while (time.AddSeconds(number) > DateTime.Now);
}
}
}
Программа создает скриншот первого элемента (Berlin) из массива Places и сохраняет его на диск.Но при выборе второго элемента из массива программа зависает при выполнении после строки:
Application.Run ();
Возможно, программа ожидает чего-то, но я не могу этого понять.
Может ли кто-нибудь мне помочь?