Skip to content

Commit

Permalink
Avoid popups. Make sure browser widht and height are greater than 0
Browse files Browse the repository at this point in the history
  • Loading branch information
jsobrier committed Feb 10, 2015
1 parent 18750f4 commit df345c5
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ public Browser()
}
}
};

this.NewWindow += delegate(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true; ;
};
}


/// <summary>
/// The frame IE is currently focused on
/// </summary>
Expand Down Expand Up @@ -198,7 +204,7 @@ public void RenderOnLoad(string fileName)
Utils.Debug("WebBrowser#DocumentCompleted");
this.Size = this.Document.Window.Size;
this.ScrollBarsEnabled = false;
Render(fileName, 1);
Render(fileName);
Console.WriteLine("Screenshot rendered to file: " + fileName);
};
}
Expand All @@ -213,6 +219,11 @@ public void Render(string filename, double ratio)
Render(filename, ratio, new Rectangle(0, 0, 0, 0));
}

public void Render(string filename)
{
Render(filename, 1.0, new Rectangle(0, 0, 0, 0));
}

/// <summary>
/// Takes a screenshot and saves into a file at a specific zoom ratio
/// </summary>
Expand All @@ -223,12 +234,30 @@ public void Render(string filename, double ratio, Rectangle clipRect)
{
using (var pic = this.Render(ratio, clipRect))
{
if (pic == null)
{
Utils.Debug("Picture could not be rendered");
return;
}

FileInfo file = new FileInfo(filename);
if (file.Exists && file.IsReadOnly)
{
Utils.Debug("File is read only: {0}", filename);
return;
}
if (Directory.Exists(filename))
{
Utils.Debug("{0} is a directory, not a file", filename);
return;
}

try
{
switch (file.Extension.Replace(".", "").ToUpper())
{
case "JPG":
case "JPEG":
pic.Save(filename, ImageFormat.Jpeg);
break;
case "GIF":
Expand All @@ -239,9 +268,10 @@ public void Render(string filename, double ratio, Rectangle clipRect)
break;
}
}
catch (Exception e)
catch (Exception ex)
{
Console.WriteLine(e.Message);
Utils.Debug(ex.Message);
Utils.Debug(ex.StackTrace);
}
}
}
Expand All @@ -265,6 +295,17 @@ public Bitmap Render(double ratio = 1.0)
/// <param name="clipRect">Part of the imaeg captured - Only Height and Width are used right now</param>
public Bitmap Render(double ratio, Rectangle clipRect)
{
if (this.Width <= 0)
{
Utils.Debug("ViewPort width is 0 or less");
return null;
}
if (this.Height <= 0)
{
Utils.Debug("ViewPort Height is 0 or less");
return null;
}

// Resize to full page size before rendering
Size oldSize = this.Size;
this.Size = PageSize;
Expand Down

0 comments on commit df345c5

Please sign in to comment.