TL;DR
画面を保存する以下の関数[1]を使ってみた。PaintDesktopとPrintWindowはWindows 7とWindows 8.1で動作に違いがあった。
BitBltはともに同じ結果が得られたため、スクリーンキャプチャに採用する関数にはBitBltが適していると考えられる。
事前知識
かなり強引に要約した。
A. スクリーン、すなわちウィンドウを含むデスクトップ
「デスクトップ」と「スクリーン」を区別しよう[2]。
「デスクトップ」は壁紙やアイコンから成る画面であり、「スクリーン」はそれらに表示されているすべてのウィンドウを加えたもの。
今回作ろうとしているキャプチャソフトは、ウィンドウを含む画面全体なので、「スクリーン」を対象とすることになる。
A. 「デスクトップ」はトップレベルウインドウの親、「シェル」はUIを提供する
「デスクトップ」ウィンドウは、すべてのトップレベルウインドウの親である[3]。
一方で、「シェル」はログオン時にUIを提供する機能を持つものである[4]。
デフォルトのシェルには「explorer.exe」が指定されている。
A. Windows 7と8.1以降ではシェルの機能が変わっている
Windows 7では、壁紙の描画をカーネルが行い、デスクトップ上のアイコンやそのキャプション、タスクバーといったものをexplorer.exeが行っていた。
しかし、Windows 8以降では壁紙の描画はexplorer.exeが行うようになった[4]。
このことから、画面を保存する3つの関数の動作が、Windows 7とWindows 8以降で異なる可能性がある。
A. DIBとDDBという異なる形式がある
ビットマップにはDIB(Device Independent Bitmap)とDDB(Device Dependent Bitmap)の二種類が存在する[5]。
DDBの特徴
- 特定のデバイスに依存する
- 原点が左上(トップダウン)
- 画素にアクセスするためには、ゲッタ・セッタ関数を用いる必要がある(安全だが遅い)
- 画像データを長方形として一まとめで扱うため、描画の計算が軽い
DIBの特徴
- 特定のデバイスに依存しない
- 原点が左下(ボトムアップ)
- 画素にアクセスするためには、メモリに直接アクセスするだけでよい(必ずしも安全ではないが速い)
- 描画の計算がDDBに比べ遅い
実施内容
今回はWindows 7, Windows 8.1のOSをそれぞれ搭載した二台のPCを用いた。画面を保存する以下の関数の動作を比較する。
開発・実行環境
Windows 7
プロセッサ: Intel(R) Core(TM) i5 CPU @2.67GHzメモリ: 4.00 GB
OS: Microsoft Windows 7 Home Premium (64bit)
compiler: Microsoft(R) C/C++ Optimizing Compiler Version 19.11.25507.1 for x86
Windows 8.1
プロセッサ: Intel(R) Core(TM) i5-5200U CPU @2.20GHz
メモリ: 8.00 GB
OS: Windows 8.1 (64bit)
compiler: Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x86
メモリ: 8.00 GB
OS: Windows 8.1 (64bit)
compiler: Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x86
共通
ライブラリ: libpng 1.6.37コード説明
テスト関数
- bool CaptureTest(TESTCASE test_case, PNGData *png_data) {
- assert(png_data);
- // The size of desktop window is acquired.
- RECT rect;
- GetWindowRect(GetDesktopWindow(), &rect);
- const int width = rect.right;
- const int height = rect.bottom;
- // Memory device context is created.
- HDC memDC = CreateCompatibleDC(NULL);
- // DIB section and DDB is created.
- BITMAPINFOHEADER bmiHeader;
- ZeroMemory(&bmiHeader, sizeof(bmiHeader));
- bmiHeader.biSize = sizeof(bmiHeader);
- bmiHeader.biWidth = width;
- bmiHeader.biHeight = height;
- bmiHeader.biPlanes = 1;
- bmiHeader.biBitCount = 24; // 24 bit BITMAP.
- BITMAPINFO bmi;
- bmi.bmiHeader = bmiHeader;
- LPVOID memDIB = NULL;
- HBITMAP memBM = CreateDIBSection(NULL, (LPBITMAPINFO)&bmi, DIB_RGB_COLORS,
- &memDIB, NULL, 0);
- // Set DDB to memory device context.
- HBITMAP prevBM = (HBITMAP)SelectObject(memDC, memBM);
- // DIB Section is set to memory DC.
- switch (test_case) {
- case TESTCASE_PAINT_DESKTOP:
- if (PaintDesktop(memDC) == 0) {
- SelectObject(memDC, prevBM);
- DeleteObject(memBM);
- DeleteDC(memDC);
- return false;
- }
- break;
- case TESTCASE_PAINT_WINDOW:
- if (PrintWindow(GetShellWindow(), memDC, 0) == 0) {
- SelectObject(memDC, prevBM);
- DeleteObject(memBM);
- DeleteDC(memDC);
- return false;
- }
- break;
- case TESTCASE_BITBLT:
- if (BitBlt(memDC, 0, 0, width, height, GetWindowDC(GetDesktopWindow()), 0,
- 0, SRCCOPY) == 0) {
- SelectObject(memDC, prevBM);
- DeleteObject(memBM);
- DeleteDC(memDC);
- return false;
- }
- break;
- default:
- break;
- }
- // DIB is converted to PNG.
- png_data->width = width;
- png_data->height = height;
- png_data->bit_depth = 8;
- png_data->rowbytes = width * 4;
- png_data->channels = 4;
- png_data->color_type = PNG_COLOR_TYPE_RGBA;
- png_data->interlace_type = PNG_INTERLACE_NONE;
- png_data->compression_type = PNG_COMPRESSION_TYPE_DEFAULT;
- png_data->filter_type = PNG_FILTER_TYPE_DEFAULT;
- png_data->red_buffer.resize(width * height);
- png_data->green_buffer.resize(width * height);
- png_data->blue_buffer.resize(width * height);
- png_data->alpha_buffer.resize(width * height);
- int bm_width = width * 3;
- if ((width * 3) % 4) {
- bm_width += (4 - (width * 3) % 4); // Set padding.
- }
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- png_data->blue_buffer[y * width + x] =
- *((LPBYTE)memDIB + (height - 1 - y) * bm_width + x * 3);
- png_data->green_buffer[y * width + x] =
- *((LPBYTE)memDIB + (height - 1 - y) * bm_width + x * 3 + 1);
- png_data->red_buffer[y * width + x] =
- *((LPBYTE)memDIB + (height - 1 - y) * bm_width + x * 3 + 2);
- png_data->alpha_buffer[y * width + x] = 255;
- }
- }
三つの関数の比較を一つの関数にまとめた。
プログラムの流れを説明する。
- スクリーンの大きさの取得
GetWindowRectを用いた。
- メモリデバイスコンテキストの取得
CreateCompatibleDCを用いた。
- ビットマップセクションの取得
メモリデバイスコンテキストにデフォルトで1x1のモノクロビットマップが選択されている[6]。
これを自前で作成したビットマップで置き換える。
そこで、CreateDIBSectionという関数を用いる。
この関数を使うことでDDBと、それに対応したDIBを同時に作成できる。
なお、CreateCompatibleBitmapという関数があるが、この関数はDDBを返す。
DDBは直接ピクセルのメモリ領域にアクセスすることができず都合が悪い。
- 画面のキャプチャ
- PaintDesktopを使う場合
指定されたデバイスコンテキストの"clipping region"(ビットマップのこと?)にデスクトップのパターンもしくは壁紙を設定する。- PaintDesktop(memDC)
- PrintWindowを使う場合
指定されたデバイスコンテキストに指定されたウィンドウの画面をコピーする。- PrintWindow(GetShellWindow(), memDC, 0)
この関数は、スクリーンキャプチャに限らず、任意のアプリケーションやコントロールのウインドウで使用できると思われる。
- BitBltを使う場合
あるデバイスコンテキストから別のデバイスコンテキストへ、矩形領域の画素をビットブロック転送する。- BitBlt(memDC, 0, 0, width, height, GetWindowDC(GetDesktopWindow()), 0, 0, SRCCOPY)
- ビットマップからPNGへの変換
PNGDataは先日の投稿[7]で作成した構造体である。
特に注意が必要なのは、画素をDIBからPNGに変換する部分。- int bm_width = width * 3;
- if ((width * 3) % 4) {
- bm_width += (4 - (width * 3) % 4); // Set padding.
- }
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- png_data->blue_buffer[y * width + x] =
- *((LPBYTE)memDIB + (height - 1 - y) * bm_width + x * 3);
- png_data->green_buffer[y * width + x] =
- *((LPBYTE)memDIB + (height - 1 - y) * bm_width + x * 3 + 1);
- png_data->red_buffer[y * width + x] =
- *((LPBYTE)memDIB + (height - 1 - y) * bm_width + x * 3 + 2);
- png_data->alpha_buffer[y * width + x] = 255;
- }
- }
![]() |
ピクセルの変換の例:サイズ2x2の画像 DIBの一列の長さが4の倍数バイトとなるようパッディングを入れる。 DIBはボトムアップ配列、PNGはトップダウン配列で画素情報を格納する。 画像のプロパティにより、この図の限りではないので注意。 |
メイン関数
呼び出しはこんな感じで行う。
- #include <stdio.h>
- #include <wchar.h>
- #include <windows.h>
- #include <windowsx.h>
- #include "./resource.h"
- #include "./util.h"
- namespace {
- constexpr wchar_t WINDOW_NAME[] = L"Template window";
- constexpr wchar_t CLASS_NAME[] = L"Template class";
- } // namespace
- int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPTSTR lpsCmdLine, int nCmdShow) {
- (void)hInstance;
- (void)hPrevInstance;
- (void)lpsCmdLine;
- (void)nCmdShow;
- #ifdef DEBUG
- FILE* fp = nullptr;
- AllocConsole();
- _wfreopen_s(&fp, L"CONOUT$", L"w", stdout);
- _wfreopen_s(&fp, L"CONOUT$", L"w", stderr);
- _wfreopen_s(&fp, L"CONIN$", L"r", stdin);
- #endif
- #ifdef DEBUG
- fwprintf(stdout, L"Hello world to stdout!\n");
- fwprintf(stderr, L"Hello world to stderr!\n");
- fwprintf(stderr, L"\n");
- #endif
- PNGData png_data;
- // Test case 1 : Use of PaintDesktop().
- if (!CaptureTest(TESTCASE_PAINT_DESKTOP, &png_data)) {
- MessageBox(NULL, L"Case 1 failed.", L"Error", MB_OK);
- } else {
- WritePNGFile(L"case_1.png", png_data);
- }
- // Test case 2 : Use of PaintWindow().
- if (!CaptureTest(TESTCASE_PAINT_WINDOW, &png_data)) {
- MessageBox(NULL, L"Case 2 failed.", L"Error", MB_OK);
- } else {
- WritePNGFile(L"case_2.png", png_data);
- }
- // Test case 3 : Use of BitBlt().
- if (!CaptureTest(TESTCASE_BITBLT, &png_data)) {
- MessageBox(NULL, L"Case 3 failed.", L"Error", MB_OK);
- } else {
- WritePNGFile(L"case_3.png", png_data);
- }
- // Everything is done!
- MessageBox(NULL, L"End of test.", L"main.exe", MB_OK);
- DestroyWindow(NULL);
- #ifdef DEBUG
- FreeConsole();
- #endif
- return 0;
- }
注意点
実行の際には、Windows 8.1では、main.exeを右クリックし、プロパティを選択、互換性のタブを開き、「高DPI設定では画面のスケーリングを無効にする」にチェックを入れた。結果と考察
以下の結果が得られた。
めでたしめでたし。
PaintDesktopとPrintWindowがうまくいかなかった原因は不明である。
- PaintDesktopを用いた場合の画像
- Windows 7: 関数が失敗した
- Windows 8.1: 壁紙のみ得られた
Windows 8で壁紙のみが取得できたのは、リファレンスにある通りの動作。
環境を変えると動かなくなる。嫌なパターンだ。
環境を変えると動かなくなる。嫌なパターンだ。
- PrintWindowを用いた場合の画像
- Windows 7: 壁紙とアイコンが得られた
- Windows 8.1: 壁紙のみ得られた
文献[4]には、Windows 7では壁紙の描画がカーネルで行われているとある。
また、デスクトップのアイコンとキャプションのみが映ったという例もある[8]。
また、デスクトップのアイコンとキャプションのみが映ったという例もある[8]。
そのため、Windows 7はカーネルが担当する壁紙は映らずに、シェルが担当するであろうアイコンのみが映り、Windows 8ではシェルが担当する壁紙とアイコンの両方が映るのだろうと予測していた。
しかし、実際は違った。
Windows 7でアイコンが映りこみ、Windows 8.1壁紙が映りこむことは予想された。
しかし、Windows 7で壁紙が映りこみ、Windows 8.1ではアイコンが映らないのは予想されなかった。
この原因の追究は一旦、保留としておく。
- BitBltを用いた場合の画像
- Windows 7 & Windows 8.1:壁紙とアイコンに加え、すべてのウィンドウが映った
めでたしめでたし。
PaintDesktopとPrintWindowがうまくいかなかった原因は不明である。
まず考えられるのは、「お前の環境が悪い」ケースである。
PaintDesktopのようなわかりやすい関数がWindows 7で失敗しているのは怪しい。
一方で、OSやAPIが壊れている可能性は微粒子レベルかもしれないが、無いとは言い切れない。
特に、今回使用したWindows 7搭載のPCはかなり古いのだ。
PaintDesktopのようなわかりやすい関数がWindows 7で失敗しているのは怪しい。
一方で、OSやAPIが壊れている可能性は微粒子レベルかもしれないが、無いとは言い切れない。
特に、今回使用したWindows 7搭載のPCはかなり古いのだ。
まとめ
- PaintDesktopはWindows 7で失敗した
- PrintWindowはWindows 7とWindows 8.1で動作に違いがあった
- BitBltはWindows 7とWindows 8.1でスクリーンのキャプチャに成功した
よって、BitBltが一番安心できる。
感想
ある環境では動いて、ある環境では動かないという、後味が悪い結果となってしまった。
とはいえ、BitBltがしっかり動くのだということが分かったのは大きな収穫だ。
これでキャプチャソフトの完成に一歩近づいた。
とはいえ、BitBltがしっかり動くのだということが分かったのは大きな収穫だ。
これでキャプチャソフトの完成に一歩近づいた。
GitHub
今回使用したコードはここに上げておく(タグ:"v_scr_captured")。参考文献
- ビットマップ / スクリーンキャプチャ
- スクリーンキャプチャ
- デスクトップウィンドウとシェルウィンドウ
- Windows 10 でのシェルの置き換えについて
- Windows bitmap
- DDBを作る
- キャプチャソフト開発 その5 libpngを使ってみる
- デスクトップのスナップショット
0 件のコメント:
コメントを投稿
コメント表示は承認制に設定しています