今天在群里有人问我,“怎么把一张图片的一半保存到另外一个文件中?”

其实只要大家熟悉图片的处理方法,就可以很轻松的解决这个问题。在VC里面,有个CImage类,用它处理这个问题很简单。不过在使用CImage类时注意添加头文件。

  1. #include <atlp_w_picpath.h> 

之后我们在需要的地方加入下面代码:

 

  1. CString filepathname = "D:\\1.png",filepathname1="D:\\11122.png";  
  2.     int width=0,height=0;  
  3.     CImage p_w_picpath,p_w_picpath1;  
  4.     p_w_picpath.Load(filepathname); //加载图片  
  5.     width=p_w_picpath.GetWidth();  
  6.     height=p_w_picpath.GetHeight();  
  7.     p_w_picpath1.Create(width,height/2,p_w_picpath.GetBPP()); // 创建一个目标存储对象 
  8.     p_w_picpath.BitBlt(p_w_picpath1.GetDC(),0,0,width,height/2,0,0,SRCCOPY);  //COPY原图的一半到目标对象里
  9.     p_w_picpath1.Save(filepathname1);  // 保存处理后的图片
  10.     p_w_picpath1.ReleaseDC();   // 释放资源
  11.     p_w_picpath1.Destroy();  // 销毁资源

PS:我这里写的图片是绝对位置。你们可以自己修改。