MFC指定位置截屏并保存为png格式 (pdf函数可以加载这种方式生成的图片)

函数定义部分:

//截屏函数
//参数一       生成图片的具体范围
//dst.left    全屏范围内X轴的起始位置(从左到右)
//dst.top     全屏范围内Y轴的起始位置(从上到下)
//dst.right   全屏范围内X轴的结束位置
//dst.bottom  全屏范围内Y轴的结束位置

//参数二       图片保存路径加文件名(C:\\Users\\Administrator\\Desktop\\CaptureScreen.png)
bool CaptureScreenPng(CRect dst,CString Path);

函数实现部分:

bool CTestCaptureScreenDlg::CaptureScreenPng(CRect dst,CString Path)
{
    HDC hdcSrc = ::GetDC(NULL);
    int nBitPerPixel = GetDeviceCaps(hdcSrc, BITSPIXEL);
    int nWidth = GetDeviceCaps(hdcSrc, HORZRES);
    int nHeight = GetDeviceCaps(hdcSrc, VERTRES);
    CImage image;
    image.Create(nWidth, nHeight, nBitPerPixel);
    BitBlt(image.GetDC(), 0, 0, nWidth, nHeight, hdcSrc, 0, 0, SRCCOPY);

    CRect rect;
    rect.left = 0;
    rect.right = (dst.right - dst.left);
    rect.top = 0;
    rect.bottom = (dst.bottom - dst.top);

    //修改图片大小
    CImage image1;
    image1.Create(rect.right,rect.bottom, nBitPerPixel);

    image.StretchBlt(image1.GetDC(),rect,dst);
    ::ReleaseDC(NULL,hdcSrc);
    image.ReleaseDC();
    image1.ReleaseDC();
    image1.Save(Path, Gdiplus::ImageFormatPNG);//ImageFormatJPEG
    image1.Destroy();
    return true;
}

生成效果图:
这里写图片描述

测试工程下载地址:MFC框架截屏指定位置并保存为png格式

猜你喜欢

转载自blog.csdn.net/a29562268/article/details/81751103