把源程序和程序运行时输人数据的界面和运行结果界面截图保存为ipg图片,保存到“C 吾言”文件夹中中

把源程序和程序运行时输人数据的界面和运行结果界面截图保存为ipg图片,保存到“C
吾言”文件夹中

【以下回答由 GPT 生成】

解决方案

首先,需要使用C语言中的图形库来获取程序运行时的界面截图。由于不同操作系统和编译器可能使用不同的图形库,这里我们以Windows操作系统和MinGW编译器为例,使用windows.h库中的相关函数来实现。

以下是具体的解决方案步骤:

  1. 引入所需的头文件:
#include <stdio.h>
#include <windows.h>
  1. 定义需要使用的函数:
// 获取程序运行时的窗口句柄
HWND GetConsoleWindow();

// 编写函数来将窗口截图保存为图片
void CaptureConsoleScreenshot(const char* filePath);
  1. 实现获取窗口句柄的函数:
HWND GetConsoleWindow() {
    return GetForegroundWindow();
}
  1. 实现将窗口截图保存为图片的函数:
void CaptureConsoleScreenshot(const char* filePath) {
    HWND consoleWindow = GetConsoleWindow();
    HDC hdcScreen = GetDC(NULL);
    HDC hdcWindow = GetDC(consoleWindow);

    RECT rect;
    GetWindowRect(consoleWindow, &rect);
    int width = rect.right - rect.left;
    int height = rect.bottom - rect.top;

    HBITMAP hBitmap = CreateCompatibleBitmap(hdcScreen, width, height);
    HDC hdcMem = CreateCompatibleDC(hdcScreen);
    HBITMAP hOldBitmap = SelectObject(hdcMem, hBitmap);

    BitBlt(hdcMem, 0, 0, width, height, hdcWindow, 0, 0, SRCCOPY);
    hBitmap = (HBITMAP)SelectObject(hdcMem, hOldBitmap);

    BITMAP bitmap;
    GetObject(hBitmap, sizeof(BITMAP), &bitmap);

    BITMAPFILEHEADER bmfHeader;
    BITMAPINFOHEADER bi;
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = bitmap.bmWidth;
    bi.biHeight = bitmap.bmHeight;
    bi.biPlanes = 1;
    bi.biBitCount = bitmap.bmBitsPixel;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = ((bitmap.bmWidth * bi.biBitCount + 31) / 32) * 4 * bitmap.bmHeight;
    bi.biXPelsPerMeter = 0;
    bi.biYPelsPerMeter = 0;
    bi.biClrUsed = 0;
    bi.biClrImportant = 0;

    DWORD dwBmpSize = ((bitmap.bmWidth * bi.biBitCount + 31) / 32) * 4 * bitmap.bmHeight;
    HANDLE hDIB = GlobalAlloc(GHND, dwBmpSize);
    char* lpbitmap = (char*)GlobalLock(hDIB);

    // 获取位图数据
    GetDIBits(hdcWindow, hBitmap, 0, (UINT)bitmap.bmHeight, lpbitmap, (BITMAPINFO*)&bi, DIB_RGB_COLORS);

    // 创建文件并保存位图数据为图片文件
    FILE* filePtr;
    fopen_s(&filePtr, filePath, "wb");
    if (filePtr == NULL) {
        printf("文件创建失败\n");
        return;
    }

    // 写入位图文件头信息和位图数据
    bmfHeader.bfReserved1 = 0;
    bmfHeader.bfReserved2 = 0;
    bmfHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwBmpSize; 
    bmfHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    fwrite(&bmfHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
    fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, filePtr);
    fwrite(lpbitmap, dwBmpSize, 1, filePtr);

    // 释放资源
    GlobalUnlock(hDIB);
    GlobalFree(hDIB);
    fclose(filePtr);
    DeleteObject(hBitmap);
    DeleteDC(hdcMem);
    ReleaseDC(consoleWindow, hdcWindow);
    ReleaseDC(NULL, hdcScreen);

    printf("截图已保存至:%s\n", filePath);
}
  1. 在主函数中调用截图函数并保存:
int main() {
    const char* filePath = "C:\\吾言\\screenshot.jpg";
    CaptureConsoleScreenshot(filePath);

    return 0;
}

以上就是使用C语言实现将程序运行时的界面截图保存为jpg图片,并将其保存到指定文件夹的解决方案。请确保在运行程序时有权限创建文件和文件夹,并且"C:\吾言"文件夹已存在。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^