如何捕获部分屏幕并将其保存为BMP
How to capture part of the screen and save it to a BMP?
可能重复:
如何在c++中用win32制作屏幕截图?
我目前正在尝试创建一个应用程序,将屏幕的一部分保存为bmp。我找到了BitBlt,但我真的不知道该怎么办。我试着搜索了一些答案,但我仍然没有用C++找到一个明确的答案。
所以,基本上我想要这个功能:
bool capturePartScreen(int x, int y, int w int, h, string dest){
//Capture part of screen according to coordinates, width and height.
//Save that captured image as a bmp to dest.
//Return true if success, false if failure
}
BitBlt:
BOOL BitBlt(
__in HDC hdcDest,
__in int nXDest,
__in int nYDest,
//The three above are the ones I don't understand!
__in int nWidth,
__in int nHeight,
__in HDC hdcSrc,
__in int nXSrc,
__in int nYSrc,
__in DWORD dwRop
);
hdc应该是什么?如何获取bmp?
花了一段时间,但我现在终于得到了一个正常运行的脚本。
要求:
#include <iostream>
#include <ole2.h>
#include <olectl.h>
此外,您可能(?)必须将ole32、oleout32和uuid添加到您的链接器中。
screenCapturePart:
bool screenCapturePart(int x, int y, int w, int h, LPCSTR fname){
HDC hdcSource = GetDC(NULL);
HDC hdcMemory = CreateCompatibleDC(hdcSource);
int capX = GetDeviceCaps(hdcSource, HORZRES);
int capY = GetDeviceCaps(hdcSource, VERTRES);
HBITMAP hBitmap = CreateCompatibleBitmap(hdcSource, w, h);
HBITMAP hBitmapOld = (HBITMAP)SelectObject(hdcMemory, hBitmap);
BitBlt(hdcMemory, 0, 0, w, h, hdcSource, x, y, SRCCOPY);
hBitmap = (HBITMAP)SelectObject(hdcMemory, hBitmapOld);
DeleteDC(hdcSource);
DeleteDC(hdcMemory);
HPALETTE hpal = NULL;
if(saveBitmap(fname, hBitmap, hpal)) return true;
return false;
}
保存位图:
bool saveBitmap(LPCSTR filename, HBITMAP bmp, HPALETTE pal)
{
bool result = false;
PICTDESC pd;
pd.cbSizeofstruct = sizeof(PICTDESC);
pd.picType = PICTYPE_BITMAP;
pd.bmp.hbitmap = bmp;
pd.bmp.hpal = pal;
LPPICTURE picture;
HRESULT res = OleCreatePictureIndirect(&pd, IID_IPicture, false,
reinterpret_cast<void**>(&picture));
if (!SUCCEEDED(res))
return false;
LPSTREAM stream;
res = CreateStreamOnHGlobal(0, true, &stream);
if (!SUCCEEDED(res))
{
picture->Release();
return false;
}
LONG bytes_streamed;
res = picture->SaveAsFile(stream, true, &bytes_streamed);
HANDLE file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, 0,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (!SUCCEEDED(res) || !file)
{
stream->Release();
picture->Release();
return false;
}
HGLOBAL mem = 0;
GetHGlobalFromStream(stream, &mem);
LPVOID data = GlobalLock(mem);
DWORD bytes_written;
result = !!WriteFile(file, data, bytes_streamed, &bytes_written, 0);
result &= (bytes_written == static_cast<DWORD>(bytes_streamed));
GlobalUnlock(mem);
CloseHandle(file);
stream->Release();
picture->Release();
return result;
}
您可以使用GetDC(NULL)
获取整个屏幕的设备上下文,然后将其与BitBlt
一起用作源设备上下文。
至于其他要做的事情:
位图创建(Windows)
位图存储(Windows)
相关文章:
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 在C++中将类(带有Vector成员)保存为二进制文件
- C++.我想将更改后的特定字符行保存在字符串中
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- 将RGB图像保存为PPM格式
- 哈希文件递归并保存到矢量Cryptopp中
- BMP的完整屏幕截图.与块状和保存有关的问题
- 使用 std::vector 保存 BMP 像素图
- 在BMP文件中保存屏幕截图的设备上下文
- 使用C++(VS 2010)FAST将BMP保存为PNG
- 如何将图形保存到 bmp
- 如何捕获部分屏幕并将其保存为BMP
- 不知道如何保存 BMP 图像
- 使用BMP-加载和保存
- 如何保存24位BMP