Graphics.CopyFromScreen Method

Definition

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

Overloads

CopyFromScreen(Point, Point, Size)

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

CopyFromScreen(Point, Point, Size, CopyPixelOperation)

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

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

CopyFromScreen(Int32, Int32, Int32, Int32, Size, CopyPixelOperation)

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.

CopyFromScreen(Point, Point, Size)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

C#
public void CopyFromScreen(System.Drawing.Point upperLeftSource, System.Drawing.Point upperLeftDestination, System.Drawing.Size blockRegionSize);

Parameters

upperLeftSource
Point

The point at the upper-left corner of the source rectangle.

upperLeftDestination
Point

The point at the upper-left corner of the destination rectangle.

blockRegionSize
Size

The size of the area to be transferred.

Exceptions

The operation failed.

Examples

The following example demonstrates how to use the CopyFromScreen method. To run this example, paste it into a Windows Form. Handle the form's Paint event and call the CopyPixels1 method from the Paint event-handling method, passing e as PaintEventArgs.

C#
private void CopyPixels1(PaintEventArgs e)
{
    e.Graphics.CopyFromScreen(this.Location, 
        new Point(40, 40), new Size(100, 100));
}

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.

See also

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

CopyFromScreen(Point, Point, Size, CopyPixelOperation)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

C#
public void CopyFromScreen(System.Drawing.Point upperLeftSource, System.Drawing.Point upperLeftDestination, System.Drawing.Size blockRegionSize, System.Drawing.CopyPixelOperation copyPixelOperation);

Parameters

upperLeftSource
Point

The point at the upper-left corner of the source rectangle.

upperLeftDestination
Point

The point at the upper-left corner of the destination rectangle.

blockRegionSize
Size

The size of the area to be transferred.

copyPixelOperation
CopyPixelOperation

One of the CopyPixelOperation values.

Exceptions

copyPixelOperation is not a member of CopyPixelOperation.

The operation failed.

Examples

The following example demonstrates how to use the CopyFromScreen method. To run this example, paste it into a Windows Form. Handle the form's Paint event and call the CopyPixels2 method from the Paint event-handling method, passing e as PaintEventArgs.

C#
private void CopyPixels2(PaintEventArgs e)
{
    e.Graphics.CopyFromScreen(this.Location, new Point(40, 40), 
        new Size(100, 100), CopyPixelOperation.MergePaint); 
}

Remarks

The CopyFromScreen methods are useful for layering one image on top of another. The copyPixelOperation parameter allows you to specify if and how the source colors should be blended with the colors in the destination area.

See also

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

CopyFromScreen(Int32, Int32, Int32, Int32, Size)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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.

C#
public void CopyFromScreen(int sourceX, int sourceY, int destinationX, int destinationY, System.Drawing.Size blockRegionSize);

Parameters

sourceX
Int32

The x-coordinate of the point at the upper-left corner of the source rectangle.

sourceY
Int32

The y-coordinate of the point at the upper-left corner of the source rectangle.

destinationX
Int32

The x-coordinate of the point at the upper-left corner of the destination rectangle.

destinationY
Int32

The y-coordinate of the point at the upper-left corner of the destination rectangle.

blockRegionSize
Size

The size of the area to be transferred.

Exceptions

The operation failed.

Examples

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

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

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.

See also

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

CopyFromScreen(Int32, Int32, Int32, Int32, Size, CopyPixelOperation)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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.

C#
public void CopyFromScreen(int sourceX, int sourceY, int destinationX, int destinationY, System.Drawing.Size blockRegionSize, System.Drawing.CopyPixelOperation copyPixelOperation);

Parameters

sourceX
Int32

The x-coordinate of the point at the upper-left corner of the source rectangle.

sourceY
Int32

The y-coordinate of the point at the upper-left corner of the source rectangle.

destinationX
Int32

The x-coordinate of the point at the upper-left corner of the destination rectangle.

destinationY
Int32

The y-coordinate of the point at the upper-left corner of the destination rectangle.

blockRegionSize
Size

The size of the area to be transferred.

copyPixelOperation
CopyPixelOperation

One of the CopyPixelOperation values.

Exceptions

copyPixelOperation is not a member of CopyPixelOperation.

The operation failed.

Examples

The following example demonstrates how to use the CopyFromScreen method. To run this example, paste it into a Windows Form. Handle the form's Paint event and call the CopyPixels4 method from the Paint event-handling method, passing e as PaintEventArgs.

C#
private void CopyPixels4(PaintEventArgs e)
{
    e.Graphics.CopyFromScreen(0, 0, 20, 20, new Size(160, 160), 
        CopyPixelOperation.SourceInvert);
}

Remarks

The CopyFromScreen methods are useful for layering one image on top of another. The copyPixelOperation parameter allows you to specify if and how the source colors should be blended with the colors in the destination area.

See also

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9