.NET Framework Class Library
Graphics..::.CopyFromScreen Method (Int32, Int32, Int32, Int32, Size)

Performs a bit-block transfer of the color data, corresponding to a rectangle of pixels, from the screen to the drawing surface of the Graphics.

Namespace:  System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)
Syntax

Visual Basic (Declaration)
Public Sub CopyFromScreen ( _
    sourceX As Integer, _
    sourceY As Integer, _
    destinationX As Integer, _
    destinationY As Integer, _
    blockRegionSize As Size _
)
Visual Basic (Usage)
Dim instance As Graphics
Dim sourceX As Integer
Dim sourceY As Integer
Dim destinationX As Integer
Dim destinationY As Integer
Dim blockRegionSize As Size

instance.CopyFromScreen(sourceX, sourceY, _
    destinationX, destinationY, blockRegionSize)
C#
public void CopyFromScreen(
    int sourceX,
    int sourceY,
    int destinationX,
    int destinationY,
    Size blockRegionSize
)
Visual C++
public:
void CopyFromScreen(
    int sourceX, 
    int sourceY, 
    int destinationX, 
    int destinationY, 
    Size blockRegionSize
)
JScript
public function CopyFromScreen(
    sourceX : int, 
    sourceY : int, 
    destinationX : int, 
    destinationY : int, 
    blockRegionSize : Size
)

Parameters

sourceX
Type: System..::.Int32
The x-coordinate of the point at the upper-left corner of the source rectangle.
sourceY
Type: System..::.Int32
The y-coordinate of the point at the upper-left corner of the source rectangle.
destinationX
Type: System..::.Int32
The x-coordinate of the point at the upper-left corner of the destination rectangle.
destinationY
Type: System..::.Int32
The y-coordinate of the point at the upper-left corner of the destination rectangle.
blockRegionSize
Type: System.Drawing..::.Size
The size of the area to be transferred.
Exceptions

ExceptionCondition
Win32Exception

The operation failed.

Remarks

The CopyFromScreen methods are useful for layering one image on top of another. To specify how the source and destination colors are blended, use one of the CopyFromScreen methods that takes a CopyPixelOperation parameter.

Examples

The following code example demonstrates how to use the CopyFromScreen for printing a copy of the current form.

Visual Basic
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Printing

Public Class Form1
    Inherits Form
    Private WithEvents printButton As New Button
    Private WithEvents printDocument1 As New PrintDocument

    Public Sub New()
        printButton.Text = "Print Form"
        Me.Controls.Add(printButton)
    End Sub

    Dim memoryImage As Bitmap

    Private Sub CaptureScreen()
        Dim myGraphics As Graphics = Me.CreateGraphics()
        Dim s As Size = Me.Size
        memoryImage = New Bitmap(s.Width, s.Height, myGraphics)
        Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
        memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, s)
    End Sub

    Private Sub printDocument1_PrintPage(ByVal sender As System.Object, _
       ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _
       printDocument1.PrintPage
        e.Graphics.DrawImage(memoryImage, 0, 0)
    End Sub

    Private Sub printButton_Click(ByVal sender As System.Object, ByVal e As _
       System.EventArgs) Handles printButton.Click
        CaptureScreen()
        printDocument1.Print()
    End Sub

    Public Shared Sub Main()
        Application.Run(New Form1())
    End Sub
End Class
C#
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;

public class Form1 :
    Form
{
    private Button printButton = new Button();
    private PrintDocument printDocument1 = new PrintDocument();

    public Form1()
    {
        printButton.Text = "Print Form";
        printButton.Click += new EventHandler(printButton_Click);
        printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
        this.Controls.Add(printButton);
    }

    void printButton_Click(object sender, EventArgs e)
    {
        CaptureScreen();
        printDocument1.Print();
    }


    Bitmap memoryImage;

    private void CaptureScreen()
    {
        Graphics myGraphics = this.CreateGraphics();
        Size s = this.Size;
        memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
    }

    private void printDocument1_PrintPage(System.Object sender,  
           System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(memoryImage, 0, 0);
    }



    public static void Main()
    {
        Application.Run(new Form1());
    }
}
.NET Framework Security

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Other Resources

Tags :


Page view tracker