今天在群里有人问我,“怎么把一张图片的一半保存到另外一个文件中?”
其实只要大家熟悉图片的处理方法,就可以很轻松的解决这个问题。在VC里面,有个CImage类,用它处理这个问题很简单。不过在使用CImage类时注意添加头文件。
- #include <atlp_w_picpath.h>
之后我们在需要的地方加入下面代码:
- CString filepathname = "D:\\1.png",filepathname1="D:\\11122.png";
- int width=0,height=0;
- CImage p_w_picpath,p_w_picpath1;
- p_w_picpath.Load(filepathname); //加载图片
- width=p_w_picpath.GetWidth();
- height=p_w_picpath.GetHeight();
- p_w_picpath1.Create(width,height/2,p_w_picpath.GetBPP()); // 创建一个目标存储对象
- p_w_picpath.BitBlt(p_w_picpath1.GetDC(),0,0,width,height/2,0,0,SRCCOPY); //COPY原图的一半到目标对象里
- p_w_picpath1.Save(filepathname1); // 保存处理后的图片
- p_w_picpath1.ReleaseDC(); // 释放资源
- p_w_picpath1.Destroy(); // 销毁资源
PS:我这里写的图片是绝对位置。你们可以自己修改。