当前位置:C++技术网 > 资讯 > Graphics中的Clone方法浅析

Graphics中的Clone方法浅析

更新时间:2015-10-08 21:14:53浏览次数:1+次

克隆复制是GDI+中的新概念,对图片克隆的范围可以是整体,也可以是局部的。实现克隆的办法是使用Bitmap类/Image类的Clone成员函数;需要注意的是,Bitmap类可以实现局部克隆和整体克隆,而Image类只能整体克隆;
我们看下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));  

看结果: