参考https://blog.csdn.net/weixin_44478077/article/details/126222971
https://blog.csdn.net/wwkaven/article/details/30095227
int CStartBarDlg::CopyBitmapToClipboard(const CString& name, CWnd* wnd, BOOL FullWnd)
{
int reVal = 0;
CRect rect;
if(FullWnd)
wnd->GetWindowRect(&rect);
else
wnd->GetClientRect(&rect);
int left = rect.left;
int top = rect.top;
CPaintDC dc(wnd);
CDC memDC;
memDC.CreateCompatibleDC(&dc);
CBitmap memBitmap, * oldmemBitmap; // 建立和屏幕兼容的bitmap
memBitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
oldmemBitmap = memDC.SelectObject(&memBitmap);//将memBitmap选入内存DC
memDC.BitBlt(0, 0, rect.Width(), rect.Height(), &dc, left, top, SRCCOPY); // 调解高度宽度
BITMAP bmp;
memBitmap.GetBitmap(&bmp); // 获得位图信息
CFile file;
if (!file.Open(name, CFile::modeWrite | CFile::modeCreate))
{
return -1;
}
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;
file.Write(&bfh, sizeof(BITMAPFILEHEADER));//写入位图文件头
file.Write(&bih, sizeof(BITMAPINFOHEADER)); //写入位图信息头
byte* p = new byte[bmp.bmWidthBytes * bmp.bmHeight]; //申请内存保存位图数据
GetDIBits(memDC.m_hDC, (HBITMAP)memBitmap.m_hObject, 0, rect.Height(), p,
(LPBITMAPINFO)&bih, DIB_RGB_COLORS); //获取位图数据
file.Write(p, bmp.bmWidthBytes * bmp.bmHeight); //写入位图数据
delete[] p;
file.Close();
memDC.SelectObject(oldmemBitmap);
memDC.DeleteDC();
return reVal;
}
文章评论