我们看下Bitmap类的Clone函数:
Clone()
Clone(Rect& rect, PixelFormat format)
Clone(RectF& rect, PixelFormat format)
Clone(INT x, INT y, INT width, INT height, PixelFormat format)
Clone(REAL x, REAL y, REAL width, REAL, height, PixelFormat format)
参数说明:
rect:[in]要克隆的区域,指在原图中的坐标区域矩形;
x,y:[in]要克隆区域的X,Y坐标;
width,height:[in]要克隆区域的宽度和高度;
PixelFormat:整数指定新的位图的像素格式。PixelFormat数据类型和常量代表各种像素格式在Gdipluspixelformats.h中定义。关于常量像素格式的更多信息,请参见图像像素格式常数,通常就是PixelFormatDontCare。
看下示例(将图片通过克隆,分成四块):
Bitmap bmp(L"wlh.bmp");
INT height=bmp.GetHeight();
INT width=bmp.GetWidth();
RectF dest[4];
dest[0]=RectF(0,0,width/2,height/2);
dest[1]=RectF(width/2,0,width/2,height/2);
dest[2]=RectF(0,height/2,width/2,height/2);
dest[3]=RectF(width/2,height/2,width/2,height/2);
//把原图像分为分四块克隆
Bitmap *s[4];
s[0]=bmp.Clone(dest[0],PixelFormatDontCare);
s[1]=bmp.Clone(dest[1],PixelFormatDontCare);
s[2]=bmp.Clone(dest[2],PixelFormatDontCare);
s[3]=bmp.Clone(dest[3],PixelFormatDontCare);
//绘图
graphics.DrawImage(s[0],RectF(0,0,width/2,height/2));
graphics.DrawImage(s[1],RectF(width/2+10,0,width/2,height/2));
graphics.DrawImage(s[2],RectF(0,height/2+10,width/2,height/2));
graphics.DrawImage(s[3],RectF(width/2+10,height/2+10,width/2,height/2));
看结果: