RingDotNetfor Ring

Screens and pictures of windows

A program can ask about the screens of the computer and take a picture of a part of the screen or of one of its own controls. This lesson uses Screen, CopyFromScreen and DrawToBitmap, and reads the pixels of the result to check how a window looks.

What you will learn#

The tutorial Images made pictures from nothing. Here the pictures come from the screen and from windows. These classes are part of Windows Forms, so the lesson needs Windows.

The program#

samples/UsingDotNet/tutorials/ch10_screenshot.ring
# screens and pictures of windows - Screen, taking a picture of the screen, and a picture of a form (Windows Forms)
load "dotnet.ring"

using {
	"System"
	"System.IO"
	"System.Drawing"
	"System.Drawing.Imaging"
	"System.Windows.Forms"
}

func main

	# 1. Screen describes the monitors. The size of a screen depends on the computer, so the program prints only what is always true.
	oScreens = new Screen()
	oPrimary = oScreens.PrimaryScreen
	nWidth = oPrimary.Bounds.Width
	nHeight = oPrimary.Bounds.Height
	? "1. the primary screen has a size: " + yesNo(nWidth > 0 and nHeight > 0)
	? "   the working area (without the taskbar) fits in the screen: " + yesNo(oPrimary.WorkingArea.Width <= nWidth and oPrimary.WorkingArea.Height <= nHeight)
	? "   at least one screen: " + yesNo(len(oScreens.AllScreens.toList()) >= 1)

	# 2. A picture of a part of the screen: a bitmap, a Graphics object that draws on it, and CopyFromScreen that copies the pixels. The first
	#    two numbers are the place on the screen, the next two the place in the bitmap, and the last the size of the part.
	oShot = new Bitmap(120,80)
	oGraphics = new Graphics().FromImage(oShot)
	oGraphics.CopyFromScreen([ :sourceX = 0, :sourceY = 0, :destinationX = 0, :destinationY = 0, :blockRegionSize = new Size(120, 80) ])
	oGraphics.Dispose()
	cFile = new Path().Combine([ new Path().GetTempPath(), "ringdotnet-shot.png" ])
	oShot.Save([ :filename = cFile, :format = new ImageFormat().Png ])
	? nl + "2. a picture of the top left corner of the screen: " + oShot.Width + " x " + oShot.Height + " pixels, saved: " + yesNo(new FileInfo(cFile).Length > 0)
	new File().Delete(cFile)
	oShot.Dispose()

	# 3. A picture of a CONTROL does not need the screen: DrawToBitmap asks the control to draw itself. The picture is always the same, even
	#    when another window is in front of the form. The panel here has a red background and a label with a white background. (A picture of
	#    the form itself also has its title bar and its border.)
	oForm = uiForm("Picture me",300,200)
	oPanel = uiPanel(oForm,10,10,240,120)
	oRed = new Color().FromArgb([ :alpha = 255, :red = 220, :green = 40, :blue = 40 ])
	oPanel.BackColor = oRed
	oLabel = uiLabel(oPanel,"Hello",20,20,100,24)
	oWhite = new Color().FromArgb([ :alpha = 255, :red = 255, :green = 255, :blue = 255 ])
	oLabel.BackColor = oWhite
	oForm.Show(NULL)
	nPanelWidth = oPanel.Width
	nPanelHeight = oPanel.Height
	oPicture = new Bitmap(nPanelWidth,nPanelHeight)
	oPanel.DrawToBitmap(oPicture,new Rectangle(0,0,nPanelWidth,nPanelHeight))
	? nl + "3. a picture of the panel: " + oPicture.Width + " x " + oPicture.Height + " pixels"

	# 4. What is in the picture? GetPixel reads one pixel, so the program can check what the panel looks like. A pixel far from the label is
	#    the red of the panel, a pixel inside the label (but not on its text) is the white of the label.
	? nl + "4. a pixel on the background: " + oPicture.GetPixel(200,100).Name
	? "   a pixel inside the label:  " + oPicture.GetPixel(110,30).Name
	? "   (the color is shown as alpha, red, green and blue in hexadecimal)"

	# 5. Count the pixels of each color: how much of the panel is background? (Every fourth pixel is enough for an estimate.)
	nRed = 0
	nTotal = 0
	for x = 0 to nPanelWidth - 1 step 4
		for y = 0 to nPanelHeight - 1 step 4
			nTotal++
			if oPicture.GetPixel(x,y).Name = "ffdc2828"
				nRed++
			ok
		next
	next
	? nl + "5. most of the panel is the red background: " + yesNo(nRed / nTotal > 0.8)

	oPicture.Dispose()
	oForm.Close()

func yesNo lValue

	if lValue
		return "yes"
	ok
	return "no"
Output
1. the primary screen has a size: yes
   the working area (without the taskbar) fits in the screen: yes
   at least one screen: yes

2. a picture of the top left corner of the screen: 120 x 80 pixels, saved: yes

3. a picture of the panel: 240 x 120 pixels

4. a pixel on the background: ffdc2828
   a pixel inside the label:  ffffffff
   (the color is shown as alpha, red, green and blue in hexadecimal)

5. most of the panel is the red background: yes
Note

This program uses a generic function (dnStatic) where a class cannot be used. The lesson The generic functions explains them.

How it works#

Screens#

Screen.PrimaryScreen is the main monitor and Screen.AllScreens all of them. Bounds is the size of a screen and WorkingArea the part that windows use (the screen without the taskbar). The values depend on the computer, so the program checks relations that are true everywhere: the working area fits in the screen.

A picture of the screen#

A Bitmap holds the picture, and a Graphics made from it (FromImage) draws on it. CopyFromScreen copies a part of the screen into the bitmap: the place on the screen, the place in the bitmap and the size of the part. Save writes a PNG file. The picture shows what is on the screen at that moment, including other programs, so a program that takes pictures of the screen tells its users.

A picture of a control#

DrawToBitmap(bitmap, rectangle) asks a control to draw itself on a bitmap. The result does not depend on what is in front of the window. This is the method to use in the tests of a window program: the test makes the picture of a panel and looks at pixels. The picture of a form also has its title bar and border, which the system draws, so the program takes the picture of a panel to know where each pixel is.

Reading the picture#

GetPixel(x, y) gives a Color, and its Name is the color as AARRGGBB: alpha, red, green and blue in hexadecimal. ffdc2828 is a fully opaque red (220, 40, 40) and ffffffff is white. A pixel far from the label is the background; a pixel in the label, away from the text, is the label's own background. The count of step 5 looks at every fourth pixel in each direction, which is enough for an estimate and 16 times faster.

Note

Each GetPixel call crosses the bridge, so it suits a few pixels. For a whole picture read all the bytes at once with LockBits.

Release the pictures#

A Bitmap and a Graphics hold system resources, so the program calls Dispose() on them when it is done (the tutorial Releasing resources).

What you learned#

Exercises#

  1. Make a function screenshot(cFile) that saves the whole primary screen.
  2. Take a picture of a panel that has a button and check the pixel in the middle of the button.
  3. Write a test that changes the BackColor of a panel and checks that the pixels changed.
  4. Take a picture of the panel before and after a click and count the pixels that differ.

Next#

Printing

RingDotNet — use .NET classes and methods from Ring programs. Screenshots and output on these pages come from the real samples.