More Examples of Simplified Image Processing in GDI+ - Sample Seven-Cloning an Image
(Page 3 of 4 )
Clone, i.e. "copy," is a new concept introduced in GDI+. You can wholly or partially clone a picture, which is performed by the Clone() method of the Bitmap class (or other classes, such as Image). To perform a partial cloning, you have to specify the related local coordinates.
The clone function of a picture is very important in scenarios requiring special image effects. Take for example, if you want to render an image by mosaic at random, the local operation becomes of great importance. Similarly, under conditions that require rendering an image piece by piece with delay, it also needs partial image handling.
In this sample, we achieve the effect of splitting a big image into four equal pieces, then clone them, and finally render them sequentially. Now let us look at the related code, as follows:
private void Clone_Click(object sender, System.EventArgs e)
{
Graphics graphics=this.CreateGraphics();
graphics.Clear(Color.White);
Bitmap image=new Bitmap("head.bmp");
int Height=image.Height;
int Width=image.Width;
// define the four areas that are used to divide a picture
RectangleF[] block=new RectangleF[4];
block[0]=new Rectangle(0,0,Width/2,Height/2);
block[1]=new Rectangle(Width/2,0,Width/2,Height/2);
block[2]=new Rectangle(0,Height/2,Width/2,Height/2);
block[3]=new Rectangle(Width/2,Height/2,Width/2,Height/2);
// close the four parts of an image respectively
Bitmap[] s=new Bitmap[4];
s[0]=image.Clone(block[0],PixelFormat.DontCare);
s[1]=image.Clone(block[1],PixelFormat.DontCare);
s[2]=image.Clone(block[2],PixelFormat.DontCare);
s[3]=image.Clone(block[3],PixelFormat.DontCare);
// render the four parts of the given image in turn the rendering time span is 1 second
graphics.DrawImage(s[0],0,0);
// delay to achieve the effect of rendering block by block
Thread.Sleep(1000);
graphics.DrawImage(s[1],Width/2,0);
Thread.Sleep(1000);
graphics.DrawImage(s[3],Width/2,Height/2);
Thread.Sleep(1000);
graphics.DrawImage(s[2],0,Height/2);
}
Running the above code, you will get a snapshot illustrated in Figure 8.
Figure 8-the running-time snapshot of cloning four parts of an image respectively
_html_246354dc.png)
Note that although Figure 8 shows an integrated image, it is composed of four equal small scraps which are rendered every second.
Next: Sample Eight-Zooming in and out >>
More Windows Scripting Articles
More By Xianzhong Zhu