Edit

Share via


Bitmap.Clone Method

Definition

Creates a copy of the section of this Bitmap defined with a specified PixelFormat.

Overloads

Clone(RectangleF, PixelFormat)

Creates a copy of the section of this Bitmap defined with a specified PixelFormat enumeration.

Clone(Rectangle, PixelFormat)

Creates a copy of the section of this Bitmap defined by Rectangle structure and with a specified PixelFormat enumeration.

Clone(RectangleF, PixelFormat)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Creates a copy of the section of this Bitmap defined with a specified PixelFormat enumeration.

public:
 System::Drawing::Bitmap ^ Clone(System::Drawing::RectangleF rect, System::Drawing::Imaging::PixelFormat format);
public System.Drawing.Bitmap Clone (System.Drawing.RectangleF rect, System.Drawing.Imaging.PixelFormat format);
override this.Clone : System.Drawing.RectangleF * System.Drawing.Imaging.PixelFormat -> System.Drawing.Bitmap
Public Function Clone (rect As RectangleF, format As PixelFormat) As Bitmap

Parameters

rect
RectangleF

Defines the portion of this Bitmap to copy.

format
PixelFormat

Specifies the PixelFormat enumeration for the destination Bitmap.

Returns

The Bitmap that this method creates.

Exceptions

rect is outside of the source bitmap bounds.

The height or width of rect is 0.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a Bitmap from a file.

  • Clones a portion of that Bitmap.

  • Draws the cloned portion to the screen.

private:
   void Clone_Example2( PaintEventArgs^ e )
   {
      // Create a Bitmap object from a file.
      Bitmap^ myBitmap = gcnew Bitmap( "Grapes.jpg" );

      // Clone a portion of the Bitmap object.
      RectangleF cloneRect = RectangleF(0,0,100,100);
      System::Drawing::Imaging::PixelFormat format = myBitmap->PixelFormat;
      Bitmap^ cloneBitmap = myBitmap->Clone( cloneRect, format );

      // Draw the cloned portion of the Bitmap object.
      e->Graphics->DrawImage( cloneBitmap, 0, 0 );
   }
private void Clone_Example2(PaintEventArgs e)
{

    // Create a Bitmap object from a file.
    Bitmap myBitmap = new Bitmap("Grapes.jpg");

    // Clone a portion of the Bitmap object.
    RectangleF cloneRect = new RectangleF(0, 0, 100, 100);
    System.Drawing.Imaging.PixelFormat format =
        myBitmap.PixelFormat;
    Bitmap cloneBitmap = myBitmap.Clone(cloneRect, format);

    // Draw the cloned portion of the Bitmap object.
    e.Graphics.DrawImage(cloneBitmap, 0, 0);
}
Private Sub Clone_Example2(ByVal e As PaintEventArgs)

    ' Create a Bitmap object from a file.
    Dim myBitmap As New Bitmap("Grapes.jpg")

    ' Clone a portion of the Bitmap object.
    Dim cloneRect As New RectangleF(0, 0, 100, 100)
    Dim format As PixelFormat = myBitmap.PixelFormat
    Dim cloneBitmap As Bitmap = myBitmap.Clone(cloneRect, format)

    ' Draw the cloned portion of the Bitmap object.
    e.Graphics.DrawImage(cloneBitmap, 0, 0)
End Sub

Applies to

Clone(Rectangle, PixelFormat)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Creates a copy of the section of this Bitmap defined by Rectangle structure and with a specified PixelFormat enumeration.

public:
 System::Drawing::Bitmap ^ Clone(System::Drawing::Rectangle rect, System::Drawing::Imaging::PixelFormat format);
public System.Drawing.Bitmap Clone (System.Drawing.Rectangle rect, System.Drawing.Imaging.PixelFormat format);
override this.Clone : System.Drawing.Rectangle * System.Drawing.Imaging.PixelFormat -> System.Drawing.Bitmap
Public Function Clone (rect As Rectangle, format As PixelFormat) As Bitmap

Parameters

rect
Rectangle

Defines the portion of this Bitmap to copy. Coordinates are relative to this Bitmap.

format
PixelFormat

The pixel format for the new Bitmap. This must specify a value that begins with Format.

Returns

The new Bitmap that this method creates.

Exceptions

rect is outside of the source bitmap bounds.

The height or width of rect is 0.

-or-

A PixelFormat value is specified whose name does not start with Format. For example, specifying Gdi will cause an ArgumentException, but Format48bppRgb will not.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a Bitmap from a file.

  • Clones a portion of that Bitmap.

  • Draws the cloned portion to the screen.

private:
   void Clone_Example1( PaintEventArgs^ e )
   {
      // Create a Bitmap object from a file.
      Bitmap^ myBitmap = gcnew Bitmap( "Grapes.jpg" );

      // Clone a portion of the Bitmap object.
      Rectangle cloneRect = Rectangle(0,0,100,100);
      System::Drawing::Imaging::PixelFormat format = myBitmap->PixelFormat;
      Bitmap^ cloneBitmap = myBitmap->Clone( cloneRect, format );

      // Draw the cloned portion of the Bitmap object.
      e->Graphics->DrawImage( cloneBitmap, 0, 0 );
   }
private void Clone_Example1(PaintEventArgs e)
{

    // Create a Bitmap object from a file.
    Bitmap myBitmap = new Bitmap("Grapes.jpg");

    // Clone a portion of the Bitmap object.
    Rectangle cloneRect = new Rectangle(0, 0, 100, 100);
    System.Drawing.Imaging.PixelFormat format =
        myBitmap.PixelFormat;
    Bitmap cloneBitmap = myBitmap.Clone(cloneRect, format);

    // Draw the cloned portion of the Bitmap object.
    e.Graphics.DrawImage(cloneBitmap, 0, 0);
}
Private Sub Clone_Example1(ByVal e As PaintEventArgs)

    ' Create a Bitmap object from a file.
    Dim myBitmap As New Bitmap("Grapes.jpg")

    ' Clone a portion of the Bitmap object.
    Dim cloneRect As New Rectangle(0, 0, 100, 100)
    Dim format As PixelFormat = myBitmap.PixelFormat
    Dim cloneBitmap As Bitmap = myBitmap.Clone(cloneRect, format)

    ' Draw the cloned portion of the Bitmap object.
    e.Graphics.DrawImage(cloneBitmap, 0, 0)
End Sub

Applies to