Export (0) Print
Expand All

How to: Copy Images

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

The .NET Compact Framework does not support the Image.Clone method, but you can still copy images and parts of images. The following examples show how to do the following:

  • Define a method to create a bitmap.

  • Define an overloaded method to copy a bitmap or to copy part of a bitmap.

  • Call these methods and draw the images to the screen by overriding the OnPaint method of your form.

To create a bitmap

  • This method creates a bitmap for demonstration purposes.


// Creates a bitmap for copying.
private Bitmap CreateBitmap(int sideSize)
{
    Bitmap bmp = new Bitmap(sideSize, sideSize);
    Graphics g = Graphics.FromImage(bmp);

    g.FillEllipse(new SolidBrush(Color.Red), 0, 0, sideSize, sideSize);
    g.DrawLine(new Pen(Color.Black), 0, 0, sideSize, sideSize);
    g.DrawLine(new Pen(Color.Black), sideSize, 0, 0, sideSize);
    g.Dispose();

    return bmp;
}


To clone a bitmap

  • This method overload takes a source bitmap as a parameter and returns the bitmap as a copy.


// Copies the entire bitmap.
protected Bitmap CopyBitmap(Bitmap source)
{
    return new Bitmap(source);
}


To copy part of a bitmap

  • This method overload takes a Rectangle as a parameter to determine the dimensions of the part of the bitmap to return.


// Copies a part of a bitmap.
protected Bitmap CopyBitmap(Bitmap source, Rectangle part)
{
    Bitmap bmp = new Bitmap(part.Width, part.Height);
    Graphics g = Graphics.FromImage(bmp);
    g.DrawImage(source,0,0,part,GraphicsUnit.Pixel);
    g.Dispose();
    return bmp;
}


To create, copy, and draw the bitmaps

  • This OnPaint method overload calls the methods to create a bitmap and then clone and copy a part of it. It also saves the cloned bitmap to a file.


// Draws the bitmaps on the form.   
protected override void OnPaint(PaintEventArgs e)
{
    Font arialFont;
    Brush blackBrush;
    arialFont = new Font("Arial", 10, FontStyle.Regular);
    blackBrush = new SolidBrush(Color.Black);

    // Set the size of the sides of the bitmap,
    // and get one-third of it for the center bitmap.
    int sidesize = 75;
    int third = (int) sidesize/3;

    // Create bitmap.
    source = CreateBitmap(sidesize);

    // Copy entirely as a clone.
    clone = CopyBitmap(source);

    // Copy the center part of the bitmap.
    center = CopyBitmap(source, new Rectangle(third, third, third, third));

    // Save the bitmap to a file.
    clone.Save("newbitmap.bmp", ImageFormat.Bmp);

    // Draw the source, clone, and partial 
    // bitmaps vertically down the screen. 
    int y = 10;

    e.Graphics.DrawString("source bitmap:", arialFont, blackBrush, 10, y);
    y += 20;

    e.Graphics.DrawImage(source, 10, y);
    y += source.Height + 10;

    e.Graphics.DrawString("clone bitmap:", arialFont, blackBrush, 10, y);
    y += 20;

    e.Graphics.DrawImage(clone, 10, y);
    y += clone.Height + 10;

    e.Graphics.DrawString("center part of bitmap:", arialFont, blackBrush, 10, y);
    y += 20;

    e.Graphics.DrawImage(center, 10, y);
    y += center.Height + 10;

    // Dispose graphic objects.
    arialFont.Dispose();
    blackBrush.Dispose();
}


This example requires references to the following namespaces:

Note that the Font and Brush objects are explicitly disposed in the OnPaint method overload. The Graphics object returned by the Graphics property of the PaintEventArgs object is destroyed by the garbage collector and does not need to be explicitly disposed.

Show:
© 2015 Microsoft