C# Get the image from a screen shot

533 views
Skip to first unread message

Tentoes

unread,
Jul 11, 2019, 1:21:55 PM7/11/19
to Selenium Users
I have a bit of code to take a screenshot and examine an area on the screen:

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;
               
}

I really don't have any use for the file. Is there a way of getting the bitmap from the Screenshot object without saving it to a file?

Jim Evans

unread,
Jul 12, 2019, 8:20:28 AM7/12/19
to Selenium Users
You can get the data from the screenshot as an array of bytes, and you can do with that whatever processing you want, without having to save to an intermediate file first. The byte array will describe an image in PNG format.

Sample:
Screenshot screenshot = ((ITakesScreenshot)Driver).GetScreenshot();
var bytes = screenshot.AsByteArray;

Tentoes

unread,
Jul 12, 2019, 11:53:14 AM7/12/19
to Selenium Users
Well, that's half the answer. Let's see if I can find how to get the png into the bitmap, or maybe just pick the pixels out of the array.

Tentoes

unread,
Jul 12, 2019, 12:08:47 PM7/12/19
to Selenium Users
So, I think the full solution is:

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;
}


Except I can't try it 'cause something else is out right now...


On Friday, July 12, 2019 at 7:20:28 AM UTC-5, Jim Evans wrote:

Joe Ward

unread,
Jul 12, 2019, 12:55:52 PM7/12/19
to seleniu...@googlegroups.com
With respect, I don't think Jim has given you "half an answer" so much as that you didn't fully understand what the answer was. 

"You can get the data from the screenshot as an array of bytes, and you can do with that whatever processing you want, without having to save to an intermediate file first." (emphasis mine)

I also notice that, using Jim's example, var bytes = screenshot.AsByteArray; can be processed, using ImageFormat, as a BMP. So presumably you could do something like this:

bytes.SaveAsFile("filename", ImageFormat.Bmp);

Hope this helps and I haven't also misunderstood myself.


--
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.

Tentoes

unread,
Jul 12, 2019, 2:20:22 PM7/12/19
to Selenium Users
And it works great. Thanks!


On Friday, July 12, 2019 at 7:20:28 AM UTC-5, Jim Evans wrote:
Reply all
Reply to author
Forward
0 new messages