完善ImageStone中的图片裁切
注: 前阵时在做一个在线图片处理的ActiveX控件,用到了黎叔的的开源项目"ImageStone",很不错,在这里还要感谢黎叔. 帮我实现了大部份功能 ,由于客户需要裁切功能,黎叔在代码中并没有去实现.我又去Google一下也没找到合适的(MSDN上也没找到关于GDI+关于图片裁切处理的VC版本..NET的到是找了一个).无耐,自已实现吧,现在把部份代码贴出来共 享一下.
1inline void FCObjImage::Cropping(RECT reCropping) {
2 if (!IsValidImage() || (reCropping.right <= 0) || (reCropping.bottom <= 0))
3 return;
4 if((int)reCropping.right==Width()&&(int)reCropping.bottom==Height())
5 return;
6 int nNewWidth = (int)(reCropping.right - reCropping.left);
7 int nNewHeight = (int)(reCropping.bottom - reCropping.top);
8
9 // first backup image const FCObjImage imgOld(*this) ;
10 if (!Create (nNewWidth, nNewHeight, imgOld.ColorBits())) {
11 assert(false) ;
12 return ;
13 }
14
15 // duplicate palette
16 if (ColorBits() <= 8)
17 CopyPalette (imgOld) ;
18
19 const int nSpan = ColorBits() / 8 ;
20
21 for (int y=reCropping.top ; y < reCropping.bottom ; y++) {
22 BYTE * pPixel = GetBits (y-reCropping.top) ;
23 for (int x=reCropping.left ; x < reCropping.right ; x++) {
24 BYTE * pOld = imgOld.GetBits(x,y);
25 FCColor::CopyPixel (pPixel, pOld, nSpan) ;
26 pPixel += nSpan ;
27 }
28 }
29}
30
2 if (!IsValidImage() || (reCropping.right <= 0) || (reCropping.bottom <= 0))
3 return;
4 if((int)reCropping.right==Width()&&(int)reCropping.bottom==Height())
5 return;
6 int nNewWidth = (int)(reCropping.right - reCropping.left);
7 int nNewHeight = (int)(reCropping.bottom - reCropping.top);
8
9 // first backup image const FCObjImage imgOld(*this) ;
10 if (!Create (nNewWidth, nNewHeight, imgOld.ColorBits())) {
11 assert(false) ;
12 return ;
13 }
14
15 // duplicate palette
16 if (ColorBits() <= 8)
17 CopyPalette (imgOld) ;
18
19 const int nSpan = ColorBits() / 8 ;
20
21 for (int y=reCropping.top ; y < reCropping.bottom ; y++) {
22 BYTE * pPixel = GetBits (y-reCropping.top) ;
23 for (int x=reCropping.left ; x < reCropping.right ; x++) {
24 BYTE * pOld = imgOld.GetBits(x,y);
25 FCColor::CopyPixel (pPixel, pOld, nSpan) ;
26 pPixel += nSpan ;
27 }
28 }
29}
30