แก้ไข

แชร์ผ่าน


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

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

public:
 void CopyFromScreen(System::Drawing::Point upperLeftSource, System::Drawing::Point upperLeftDestination, System::Drawing::Size blockRegionSize);
public void CopyFromScreen (System.Drawing.Point upperLeftSource, System.Drawing.Point upperLeftDestination, System.Drawing.Size blockRegionSize);
member this.CopyFromScreen : System.Drawing.Point * System.Drawing.Point * System.Drawing.Size -> unit
Public Sub CopyFromScreen (upperLeftSource As Point, upperLeftDestination As Point, blockRegionSize As Size)

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.

private void CopyPixels1(PaintEventArgs e)
{
    e.Graphics.CopyFromScreen(this.Location, 
        new Point(40, 40), new Size(100, 100));
}
Private Sub CopyPixels1(ByVal e As PaintEventArgs) 
    e.Graphics.CopyFromScreen(Me.Location, _
        New Point(40, 40), New Size(100, 100))

End Sub

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

CopyFromScreen(Point, Point, Size, CopyPixelOperation)

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.

public:
 void CopyFromScreen(System::Drawing::Point upperLeftSource, System::Drawing::Point upperLeftDestination, System::Drawing::Size blockRegionSize, System::Drawing::CopyPixelOperation copyPixelOperation);
public void CopyFromScreen (System.Drawing.Point upperLeftSource, System.Drawing.Point upperLeftDestination, System.Drawing.Size blockRegionSize, System.Drawing.CopyPixelOperation copyPixelOperation);
member this.CopyFromScreen : System.Drawing.Point * System.Drawing.Point * System.Drawing.Size * System.Drawing.CopyPixelOperation -> unit
Public Sub CopyFromScreen (upperLeftSource As Point, upperLeftDestination As Point, blockRegionSize As Size, copyPixelOperation As 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.

private void CopyPixels2(PaintEventArgs e)
{
    e.Graphics.CopyFromScreen(this.Location, new Point(40, 40), 
        new Size(100, 100), CopyPixelOperation.MergePaint); 
}
Private Sub CopyPixels2(ByVal e As PaintEventArgs) 
    e.Graphics.CopyFromScreen(Me.Location, _
        New Point(40, 40), New Size(100, 100), _
        CopyPixelOperation.MergePaint)
End Sub

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

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

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.

public:
 void CopyFromScreen(int sourceX, int sourceY, int destinationX, int destinationY, System::Drawing::Size blockRegionSize);
public void CopyFromScreen (int sourceX, int sourceY, int destinationX, int destinationY, System.Drawing.Size blockRegionSize);
member this.CopyFromScreen : int * int * int * int * System.Drawing.Size -> unit
Public Sub CopyFromScreen (sourceX As Integer, sourceY As Integer, destinationX As Integer, destinationY As Integer, blockRegionSize As Size)

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.

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

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

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

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.

public:
 void CopyFromScreen(int sourceX, int sourceY, int destinationX, int destinationY, System::Drawing::Size blockRegionSize, System::Drawing::CopyPixelOperation copyPixelOperation);
public void CopyFromScreen (int sourceX, int sourceY, int destinationX, int destinationY, System.Drawing.Size blockRegionSize, System.Drawing.CopyPixelOperation copyPixelOperation);
member this.CopyFromScreen : int * int * int * int * System.Drawing.Size * System.Drawing.CopyPixelOperation -> unit
Public Sub CopyFromScreen (sourceX As Integer, sourceY As Integer, destinationX As Integer, destinationY As Integer, blockRegionSize As Size, copyPixelOperation As 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.

private void CopyPixels4(PaintEventArgs e)
{
    e.Graphics.CopyFromScreen(0, 0, 20, 20, new Size(160, 160), 
        CopyPixelOperation.SourceInvert);
}
Private Sub CopyPixels4(ByVal e As PaintEventArgs) 
    e.Graphics.CopyFromScreen(0, 0, 20, 20, _
        New Size(160, 160), CopyPixelOperation.SourceInvert)
End Sub

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