博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MFC 屏幕截图(libjpeg bmp转jpg)
阅读量:4635 次
发布时间:2019-06-09

本文共 2933 字,大约阅读时间需要 9 分钟。

项目中需要用到的功能。

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) == 0)        return;    CBitmap memBitmap, *oldmemBitmap;    if(memBitmap.CreateCompatibleBitmap(pDC, Width, Height) == NULL)        return;    oldmemBitmap = memDC.SelectObject(&memBitmap);    if(oldmemBitmap == NULL)        return;    if(memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY) == 0)        return;    CPoint po;    GetCursorPos(&po);    HICON hinco = (HICON)GetCursor();    memDC.DrawIcon(po.x-10 , po.y - 10 , hinco);    BITMAP bmp;    memBitmap.GetBitmap(&bmp);    BITMAPINFOHEADER bih = {
0}; bih.biBitCount = bmp.bmBitsPixel; bih.biCompression = BI_RGB; bih.biHeight = bmp.bmHeight; bih.biPlanes = 1; bih.biSize = sizeof(BITMAPINFOHEADER); bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight; bih.biWidth = bmp.bmWidth; BITMAPFILEHEADER bfh = {
0}; 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 , 0, 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[1];    int row_stride;    cinfo.err = jpeg_std_error(&jerr);    jpeg_create_compress(&cinfo);    DWORD dwLen = nWidth * nHeight * 3;    jpeg_mem_dest(&cinfo, ppDes, &dwLen);    cinfo.image_width = nWidth;    cinfo.image_height = nHeight;    cinfo.input_components = 3;    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 * 3;    for (int i=0, j=0; j < nWidth*nHeight*4; i+=3, j+=4)    //BGRA => RGB    {        *(pSrc+i)=*(pSrc+j+2);        *(pSrc+i+1)=*(pSrc+j+1);        *(pSrc+i+2)=*(pSrc+j);    }    while (cinfo.next_scanline < cinfo.image_height)    {        row_pointer[0] = &pSrc[(cinfo.image_height - cinfo.next_scanline - 1) * row_stride];        (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);    }    jpeg_finish_compress(&cinfo);    jpeg_destroy_compress(&cinfo);    return dwLen;}

需要用到的头文件

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

 

转载于:https://www.cnblogs.com/john-h/p/6020330.html

你可能感兴趣的文章
使用java中replaceAll方法替换字符串中的反斜杠
查看>>
Android初学第36天
查看>>
Some configure
查看>>
.net core 中的[FromBody]
查看>>
json_encode时中文编码转正常状态
查看>>
流量调整和限流技术 【转载】
查看>>
Axure 全局辅助线(转)
查看>>
正由另一进程使用,因此该进程无法访问此文件。
查看>>
1 线性空间
查看>>
VS不显示最近打开的项目
查看>>
MyEclipse安装Freemarker插件
查看>>
计算多项式的值
查看>>
DP(动态规划)
查看>>
chkconfig
查看>>
TMS320F28335项目开发记录2_CCS与JTAG仿真器连接问题汇总
查看>>
最强的篮球队和马尔可夫模型
查看>>
hdu-4302-Holedox Eating-线段树-单点更新,有策略的单点查询
查看>>
cocos2d-x 音效中断问题
查看>>
设计模式简要笔记
查看>>
子分类账知识学习(汇总网上比较有用的资料)
查看>>