After figuring out how to take a screenshot using .Net, I translated the C# I came up with to IronRuby. Now, when any of my IronRuby-powered tests fail, I take a screenshot – saves loads of time when trying to work out why a test failed!
Here’s the code you need:
require 'System.Drawing'
require 'System.Windows.Forms'
bitmap = System::Drawing::Bitmap.new(
System::Windows::Forms::Screen.PrimaryScreen.Bounds.Width,
System::Windows::Forms::Screen.PrimaryScreen.Bounds.Height,
System::Drawing::Imaging::PixelFormat.Format32bppArgb)
System::Drawing::Graphics.FromImage(bitmap).CopyFromScreen(
System::Windows::Forms::Screen.PrimaryScreen.Bounds.X,
System::Windows::Forms::Screen.PrimaryScreen.Bounds.Y,
0,
0,
System::Windows::Forms::Screen.PrimaryScreen.Bounds.Size,
System::Drawing::CopyPixelOperation.SourceCopy)
bitmap.Save("c:\\screenshot.png", System::Drawing::Imaging::ImageFormat.Png)
A screenshot is taken of the primary screen (and *only* the primary screen) and is saved as a PNG to c:\screenshot.png . Modify to your heart’s content.
Hole-in-the-open-source-market Alert: cross platform gem for taking screenshots. Please don’t make me write it!
















Have you seen what Tim did on this?
http://altentee.com/2008/capturing-screenshots-in-watir/
Great stuff. I’ve written a similar app in java before, now a .Net-dependent one in ironruby… but there still remains the problem of a multi-platform solution. A tool that takes a shot of every screen, not just the main one… It would be something very simple like:
ScreenShotApp.grab_and_save_to(“c:\\screenshot.png”)
which would also cope with:
ScreenShotApp.grab_and_save_to(“/Users/nat/screenshot.png”)
…etc…
On being called, it would figure out what platform it’s running on and use the appropriate (and well-hidden-from-the-user) platform-specific code.
Any takers?