【发布时间】:2019-01-28 10:58:30
【问题描述】:
我正在编写一项服务,其中包含本地网站网页的屏幕截图。 我正在使用 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);
}
}
}
程序从 Places 数组中创建第一个元素(柏林)的屏幕截图并将其保存到磁盘。 但是当从数组中选择第二个元素时,程序在该行之后挂起执行:
Application.Run();
也许程序需要一些东西,但我无法理解。
有人可以帮我吗?
【问题讨论】:
标签: c# .net winforms webbrowser-control