[C++] 纯文本查看 复制代码
#include <windows.h>
#include <gdiplus.h>
#include <string>
#include <iostream>
#include <locale>
#include <codecvt>
#include <shellscalingapi.h>
#include <vector>
#pragma comment (lib,"Gdiplus.lib")
#pragma comment(lib, "Shcore.lib")
using namespace Gdiplus;
using namespace std;
// 支持多种图像格式保存
class ImageSaver {
public:
ImageSaver(const string& format) : format_(format) {}
bool Save(Bitmap& bitmap, const wstring& filename) {
CLSID clsid;
Status status;
if (format_ == "png") {
CLSIDFromString(L"{557cf400-1a04-11d3-9a73-0000f81ef32e}", &clsid); // PNG
status = bitmap.Save(filename.c_str(), &clsid, NULL);
} else if (format_ == "jpg" || format_ == "jpeg") {
EncoderParameters encoderParams;
encoderParams.Count = 1;
encoderParams.Parameter[0].Guid = EncoderQuality;
encoderParams.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParams.Parameter[0].NumberOfValues = 1;
encoderParams.Parameter[0].Value = new LONG[1];
encoderParams.Parameter[0].Value[0] = 80; // 设置JPG质量为80
CLSIDFromString(L"{557cf401-1a04-11d3-9a73-0000f81ef32e}", &clsid); // JPEG
status = bitmap.Save(filename.c_str(), &clsid, &encoderParams);
delete[] encoderParams.Parameter[0].Value;
} else { // 默认使用BMP格式
status = bitmap.Save(filename.c_str(), &Bitmap::GetCodecClsid(Gdiplus::ImageFormatBmp), NULL);
}
if (status != Status::Ok) {
wcerr << L"Failed to save bitmap to file (" << format_ << "): " << filename << endl;
return false;
}
return true;
}
private:
string format_;
};
void SaveHBITMAPToFile(HBITMAP hBitmap, const wstring& filename, const string& format) {
Bitmap bitmap(hBitmap, NULL);
ImageSaver saver(format);
saver.Save(bitmap, filename);
}
BOOL CaptureScreen(const wstring& filename, int x, int y, int width, int height, const string& format) {
HDC hScreenDC = GetDC(NULL);
if (!hScreenDC) {
return FALSE;
}
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
HGDIOBJ oldBitmap = SelectObject(hMemoryDC, hBitmap);
BOOL result = BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, x, y, SRCCOPY);
if (result) {
SaveHBITMAPToFile(hBitmap, filename, format);
}
SelectObject(hMemoryDC, oldBitmap);
DeleteObject(hBitmap);
DeleteDC(hMemoryDC);
ReleaseDC(NULL, hScreenDC);
return result;
}
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM pData) {
// ... (原有代码不变)
int x = mi.rcMonitor.left;
int y = mi.rcMonitor.top;
int width = mi.rcMonitor.right - x;
int height = mi.rcMonitor.bottom - y;
vector<wstring> args;
wstring outputPath = *(wstring*)pData;
args.push_back(outputPath);
args.push_back(to_wstring(++monitorCount));
wstring filename = outputPath + L"\\screenshot_" + to_wstring(monitorCount) + L"." + to_wstring("bmp"); // 默认为bmp格式
// 假设命令行参数提供了图片格式
if (argc > 2) {
string format = argv[2];
filename = outputPath + L"\\screenshot_" + to_wstring(monitorCount) + L"." + to_wstring(format);
}
if (!CaptureScreen(filename, x, y, width, height, "bmp")) { // 默认为bmp格式,根据实际情况修改
wcerr << L"Failed to capture screen for monitor " << monitorCount << endl;
}
return TRUE;
}
int main(int argc, char* argv[]) {
if (argc < 2 || argc > 3) {
cout << "Usage: " << argv[0] << " <output_directory> [image_format: bmp/png/jpeg]" << endl;
return 1;
}
// ... (原有代码不变)
wstring outputPath = wstring_convert<codecvt_utf8<wchar_t>>().from_bytes(argv[1]);
// 检查并设置图片格式
string format = "bmp";
if (argc == 3) {
format = argv[2];
if (format != "bmp" && format != "png" && format != "jpg" && format != "jpeg") {
cerr << "Unsupported image format! Defaulting to bmp." << endl;
format = "bmp";
}
}
EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&outputPath);
// ... (原有代码不变)
return 0;
}