I would like to know if instead of void function in thread, I can pass bitmap function.
When I am passing void function there is no error reported but when I am passing the bitmap function I am getting an error.
Here is the code:
protected void btnSubmit_Click(object sender, EventArgs e)
{
Thread thr = new Thread(new ThreadStart(OptimizeWebBrowser));
thr.SetApartmentState(ApartmentState.STA);
thr.Start();
}
public System.Drawing.Bitmap CaptureWebPage(string url)
{
System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser();
browser.ScrollBarsEnabled = false;
browser.ScriptErrorsSuppressed = true;
browser.Navigate(url);
while (browser.ReadyState != WebBrowserReadyState.Complete)
System.Windows.Forms.Application.DoEvents();
System.Threading.Thread.Sleep(1500);
int width = browser.Document.Body.ScrollRectangle.Width;
int height = browser.Document.Body.ScrollRectangle.Height;
browser.Width = width;
browser.Height = height;
System.Drawing.Bitmap bmp = new Bitmap(width, height);
browser.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height));
return bmp;
}
protected void OptimizeWebBrowser()
{
System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser();
browser.ScrollBarsEnabled = false;
browser.ScriptErrorsSuppressed = true;
browser.Navigate(currentPageUrl);
while (browser.ReadyState != WebBrowserReadyState.Complete)
System.Windows.Forms.Application.DoEvents();
System.Threading.Thread.Sleep(1500);
int width = browser.Document.Body.ScrollRectangle.Width;
int height = browser.Document.Body.ScrollRectangle.Height;
browser.Width = width;
browser.Height = height;
System.Drawing.Bitmap bmp = new Bitmap(width, height);
browser.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height));
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition", "attachment; filename=screenshot.jpg");
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
bmp.Dispose();
bmp.Dispose();
Response.End();
}
When I am trying now to put CAptureWebPage instead of OptimizeWebBrowser in the ThreadStart, I am getting an error: ‘No overload for ‘CaptureWebPage’ matches delegate ‘System.Threading.ThreadStart’.
To pass a parameter to a Thread you should use
ParameterizedThreadStartdelegate which has single parameter ofobjecttype. So you should use it as shown below:PS: code which you’ve posted is very hard to read, consider some code refactoring and style changes