项目中需要用到的功能。

void Screenshot()
{
CDC *pDC;
pDC = CDC::FromHandle(GetDC(GetDesktopWindow()));
if(pDC == NULL)
return;
int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);
int Width = pDC->GetDeviceCaps(HORZRES);
int Height = pDC->GetDeviceCaps(VERTRES); CDC memDC;
if(memDC.CreateCompatibleDC(pDC) == )
return; CBitmap memBitmap, *oldmemBitmap;
if(memBitmap.CreateCompatibleBitmap(pDC, Width, Height) == NULL)
return; oldmemBitmap = memDC.SelectObject(&memBitmap);
if(oldmemBitmap == NULL)
return;
if(memDC.BitBlt(, , Width, Height, pDC, , , SRCCOPY) == )
return; CPoint po;
GetCursorPos(&po);
HICON hinco = (HICON)GetCursor();
memDC.DrawIcon(po.x- , po.y - , hinco); BITMAP bmp;
memBitmap.GetBitmap(&bmp); BITMAPINFOHEADER bih = {};
bih.biBitCount = bmp.bmBitsPixel;
bih.biCompression = BI_RGB;
bih.biHeight = bmp.bmHeight;
bih.biPlanes = ;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;
bih.biWidth = bmp.bmWidth; BITMAPFILEHEADER bfh = {};
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;
bfh.bfType = (WORD)0x4d42; BYTE* pImgBuff = new BYTE[bmp.bmWidthBytes * bmp.bmHeight]; GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject
, , Height, pImgBuff, (LPBITMAPINFO) &bih, DIB_RGB_COLORS);
memDC.SelectObject(oldmemBitmap); BYTE* pJpg = NULL;
int nJpgSize = BmpToJpeg(pImgBuff, bmp.bmWidth, bmp.bmHeight, &pJpg); if (NULL != pJpg)
{
delete[] pJpg;
} delete [] pImgBuff;
}

附上 BmpToJpeg

int BmpToJpeg(BYTE *pSrc, int nWidth, int nHeight, BYTE** ppDes)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW row_pointer[];
int row_stride;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo); DWORD dwLen = nWidth * nHeight * ;
jpeg_mem_dest(&cinfo, ppDes, &dwLen); cinfo.image_width = nWidth;
cinfo.image_height = nHeight;
cinfo.input_components = ;
cinfo.in_color_space = JCS_RGB; jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, JPEG_QUALITY, TRUE);
jpeg_start_compress(&cinfo, TRUE); row_stride = nWidth * ;
for (int i=, j=; j < nWidth*nHeight*; i+=, j+=) //BGRA => RGB
{
*(pSrc+i)=*(pSrc+j+);
*(pSrc+i+)=*(pSrc+j+);
*(pSrc+i+)=*(pSrc+j);
} while (cinfo.next_scanline < cinfo.image_height)
{
row_pointer[] = &pSrc[(cinfo.image_height - cinfo.next_scanline - ) * row_stride];
(void) jpeg_write_scanlines(&cinfo, row_pointer, );
}
jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo);
return dwLen;
}

需要用到的头文件

#define JPEG_QUALITY 50
extern "C"
{
#include "jpeglib.h"
#include "jmorecfg.h"
#include "jconfig.h"
}

MFC 屏幕截图(libjpeg bmp转jpg)的更多相关文章

  1. 远程控制编写之屏幕传输 MFC实现 屏幕截图 发送bmp数据 显示bmp图像

    远程控制编写之屏幕传输  MFC实现  屏幕截图 发送bmp数据 显示bmp图像: 一 : 首先要了解bmp图像的结构 详情请看我转载的一篇文章http://blog.csdn.net/hnust_x ...

  2. MFC对话框显示BMP图片

    1.MFC对话框显示BMP图片我们先从简单的开始吧.先分一个类: (一) 非动态显示图片(即图片先通过资源管理器载入,有一个固定ID) (二) 动态载入图片(即只需要在程序中指定图片的路径即可载入) ...

  3. MFC中显示 .bmp格式的位图

    最近在看VisualC++ 图像处理的书籍,表示一直在从基础做起,今天就记录一个简单功能的实现,显示.bmp格式的位图. 首先需要理解的是窗口创建的过程包括两个步骤:首先擦除窗口的背景,然后在对窗口进 ...

  4. MFC如何添加bmp文件和ICO文件

    1.添加BMP格式文件如下图所示: 2.添加ICO格式文件如下图所示:

  5. MFC CListCtrl 显示bmp图片

    m_ListCtrl.SetExtendedStyle(m_ListCtrl.GetExtendedStyle()| LVS_EX_SUBITEMIMAGES | LVS_EX_GRIDLINES); ...

  6. MFC 屏幕截图方法

    //获取当前屏幕的并且保存图片 LRESULT CFeetScanView::SaveViewBMP(WPARAM wParam, LPARAM lParam) { CRect rect; this- ...

  7. VS MFC 按键导入BMP图片

    1. 图片导入资源: 2.实现代码: 直接给CButton加图片的方法: 1.在资源编辑器中添加一个按钮.把它的Bitmap属性设为true 2.在按钮上点右键,添加一个变量m_Btn(CButton ...

  8. MFC对话框中显示BMP,JPG图片

    //************************************ // 方法说明:    显示JPG和GIF.BMP图片 // 参数说明:    CDC * pDC           设 ...

  9. jpg转bmp(使用libjpeg)

    源: jpg转bmp(使用libjpeg) [转]JPEG压缩原理 bmp转jpg(使用libjpeg)

随机推荐

  1. opendaylight安装

    OpenDaylight安装 环境 jdk-1.8 maven 环境配置安装 Java环境 查看Java环境 java  -version 安装jdk-1.8 yum install java-1.8 ...

  2. 内置函数系列之 filter

    filter 过滤 基本语法: s = filter(function,iterable) 将可迭代对象的每一个元素,传进函数中,根据函数中的判断条件,返回True或False 返回True的是保留的 ...

  3. Linux设置下载站点

    https://blog.csdn.net/jfhkd2012/article/details/50912757

  4. 笔记-python-built-in functions-eval,exec,compile

    笔记-python-built-in functions-eval,exec,compile 1.      python代码执行函数 有时需要动态改变代码,也就是说代码需要是字符串格式,然后在按需要 ...

  5. 有关ViewFlipper的使用及设置动画效果的讲解

    说到左右滑动,其实实现左右滑动的方式很多,有ViewPaer,自定义实现Viewgroup,gallery等都可以达到这种效果.这里做下ViewFliper实现左右滑动的效果. 会用到以下的技术: 1 ...

  6. python中字符串是特殊的列表

    for x in range(20): print 'fizz'[x%3*4::]+'buzz'[x%5*4::]or x 这个是由 Jeff Atwood推广的一个编程练习叫FizzBuzz,问题如 ...

  7. neutron floating ip 限速

    查看浮动ip的id [root@10e131e69e14 oz]# openstack floating ip show 36.111.0.197 +---------------------+--- ...

  8. Elasticsearch同义词词汇单元过滤器

    1 简单扩展 "jump,hop,leap" 搜索jump会检索出包含jump.hop或leap的词 1.1 扩展应用在索引阶段 1.2 扩展应用在查询阶段 1.3 对比 2 简单 ...

  9. CentOS 6.3下安装Vsftp,虚拟用户

    CentOS 6.3下安装Vsftp,虚拟用户一.安装:1.安装Vsftpd服务相关部件:[root@linuxidc.com ~]# yum install vsftpd*Dependencies ...

  10. 云效(阿里云)流水线 + nginx + uWsgi + flask + python3 基础环境搭建 --备忘

    一.开发环境搭建 1.安装python3 yum -y groupinstall "Development tools" yum -y install zlib-devel bzi ...