Export (0) Print
Expand All
13 out of 18 rated this helpful - Rate this topic

HOWTO: Copy Bitmap Regions

.NET Compact Framework 1.0

Geoff Schwab
Excell Data Corporation

Contributions by Michael Lipp
Microsoft Corporation

October 2003

Applies to:

   Microsoft® .NET Compact Framework 1.0
   Microsoft Visual Studio® .NET 2003

Download sample.

Summary: This sample demonstrates two functions for copying Bitmap objects.

  • static public Bitmap Copy(Bitmap srcBitmap, Rectangle section) – This function returns a Bitmap object containing the specified region of the source Bitmap object.
  • static public Bitmap Copy(Bitmap source) – This function returns a Bitmap object which is a copy of the original source Bitmap object.

Implementation

The first function consists of the following steps:

  1. Create a new Bitmap object the size of the specified section
  2. Create a Graphics object from the new Bitmap
  3. Copy the region specified by section from the Bitmap object srcBitmap into the new Bitmap using the Graphics object
  4. Dispose the Graphics object
  5. Return the new Bitmap

Code demonstrating how to copy a region of a Bitmap is shown below.

// C#
static public Bitmap Copy(Bitmap srcBitmap, Rectangle section)
{
    // Create the new bitmap and associated graphics object
    Bitmap bmp = new Bitmap(section.Width, section.Height);
    Graphics g = Graphics.FromImage(bmp);

    // Draw the specified section of the source bitmap to the new one
    g.DrawImage(srcBitmap, 0, 0, section, GraphicsUnit.Pixel);

    // Clean up
    g.Dispose();

    // Return the bitmap
    return bmp;
}

'VB
Overloads Public Shared Function Copy(srcBitmap As Bitmap, _
  section As Rectangle) As Bitmap

    ' Create the new bitmap and associated graphics object
    Dim bmp As New Bitmap(section.Width, section.Height)
    Dim g As Graphics = Graphics.FromImage(bmp)
      
    ' Draw the specified section of the source bitmap to the new one
    g.DrawImage(srcBitmap, 0, 0, section, GraphicsUnit.Pixel)
      
    ' Clean up
    g.Dispose()
      
    ' Return the bitmap
    Return bmp

End Function 'Copy

The second copy function creates a full copy of the original bitmap in two steps:

  1. The constructor of the Bitmap object is used to create a new instance which is a copy of the original Bitmap object.
  2. The new Bitmap instance is returned

Code demonstrating this simple function is below.

// C#
static public Bitmap Copy(Bitmap srcBitmap)
{
    // Simply use the constructor to create and return a copy
    return new Bitmap(srcBitmap);
}

'VB 
Public Overloads Shared Function Copy(ByVal srcBitmap As Bitmap) _
  As Bitmap

    ' Simply use the constructor to create and return a copy
    Return New Bitmap(srcBitmap)

End Function 'Copy

Conclusion

The sample included with this article encapsulates these and a third function used to create a blank Bitmap and draw to it in a MyBitmap class. Another class, Form1, is also defined in the sample. This form is responsible for demonstrating the use of the MyBitmap class by creating an original Bitmap, a clone Bitmap, and a Bitmap made up of a copied section of the original and displaying them in a Form's client area.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.