Clone Image

Image Editing Tutorials: For C# and VB NET Students


Cloning an image is making a copy of the whole image, or just a part of it. Add a new button to your form from previous lesson and we'll try it out.

The first thing to do is to create a Bitmap of our image from the PictureBox, as before:


Bitmap bmp = new Bitmap(pictureBox1.Image);


Dim bmp As Bitmap = New Bitmap(PictureBox1.Image)

The next thing you need to do is set up a rectangle object. This is used to specify the size of the cloned area. Add the following to you code in C#:

Rectangle rect = new Rectangle(0, 0, 100, 100);

And this in VB Net:

Dim rect = New Rectangle(0, 0, 100, 100)

The line above sets up a new Rectangle object called rect. The first two numbers are the starting point for the rectangle. We're specifying X and Y starting positions of 0, 0. This will get us the top left of our image. The next two numbers, (100, 100) are the width and height of the rectangle.

Now that we have a rectangle set up, we can use the Clone method of Bitmap objects. Add this rather long line to your code:


Bitmap cloneImage = bmp.Clone(rect, System.Drawing.Imaging.PixelFormat.DontCare);

VB Net

Dim cloneImage As Bitmap = bmp.Clone(rect, Imaging.PixelFormat.DontCare)

The first thing we do is to set up a new Bitmap image called cloneImage. After an equal sign we have our previous Bitmap object (bmp). This, remember, holds the image from the PictureBox. After typing a dot, add the Clone method. The Clone method takes two arguments between its round brackets. The first is a rectangle object. This is needed in order to tell Visual Studio which area of the image you want to clone. For us, this was an area 100 by 100, starting at position 0, 0.

The second argument is rather curious. It's this:


Incidentally, in C#, you can add a using statement at the top of your code.This:

using System.Drawing.Imaging;

Then that long line can be shortened to just this:


Visual Studio needs to know the pixel format for the image. This refers to the quality of each pixel in the image you want cloned. PixelFormat is part of the System.Drawing.Imaging namespace. Type a dot after PixelFormat and you'll see the following list of options:

Context menu for PixelFormat property in Visual Studio

We're not too fussy about the image quality so we've gone for the splendidly-named DontCare option.

The only other thing to do is to transfer the cloned image to the PictureBox:


pictureBox1.Image = cloneImage;


PictureBox1.Image = cloneImage

The whole of your code should now look like this in C#:

C# code to clone an image

And this in VB Net:

VB Net code to clone an image

Try it out. Make sure there is an image loaded into your PictureBox (you can add the if statement that checks for this, if you like). Click your Clone button. Before the image is cloned ours looked like this:

An image before a clone

After the clone, we're left with this

An image after a clone


In the next lesson, we'll take a look at manipulating individual pixels in your VB or C# code.

Image Pixels >>

Back to the C# NET Contents Page

Back to the VB NET Contents Page