public static Bitmap TakeScreenshot(By locator)
{
Directory.CreateDirectory("c:/trash");
string filename = "c:/trash/screenshot" + DateTime.Now.ToString("yyyy'-'MM'-'dd hh-mm-ss") + ".png";
IWebElement element = Driver.FindElement(locator);
Rectangle rect = new Rectangle(element.Location, element.Size);
Rectangle dest = new Rectangle(Point.Empty, rect.Size);
Screenshot screenshot = ((ITakesScreenshot)Driver).GetScreenshot();
screenshot.SaveAsFile(filename);
Image img = Bitmap.FromFile(filename);
Bitmap cimg = new Bitmap(rect.Width, rect.Height);
using (var graphics = Graphics.FromImage(cimg))
{
graphics.DrawImage(img, dest, rect, GraphicsUnit.Pixel);
}
return cimg;
}
Sample:
Screenshot screenshot = ((ITakesScreenshot)Driver).GetScreenshot();
var bytes = screenshot.AsByteArray;
public static Bitmap SnapScreenShot(By locator) { Screenshot ss = ((ITakesScreenshot)Driver).GetScreenshot(); Byte[] bytes = ss.AsByteArray; MemoryStream ms = new MemoryStream(bytes); Image img = Image.FromStream(ms); IWebElement element = Driver.FindElement(locator); Rectangle rect = new Rectangle(element.Location, element.Size); Rectangle dest = new Rectangle(Point.Empty, rect.Size); Bitmap cimg = new Bitmap(rect.Width, rect.Height); using (var graphics = Graphics.FromImage(cimg)) { graphics.DrawImage(img, dest, rect, GraphicsUnit.Pixel); } return cimg; }
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/481687c2-1437-4616-a0f7-79858bacaa85%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.