質問するログイン新規登録

回答編集履歴

2

実験したコードを追記

2017/12/03 22:38

投稿

Bongo
Bongo

スコア10818

answer CHANGED
@@ -1,2 +1,49 @@
1
1
  画像を見るに、[Getting a window rectangle without the drop shadow - Articles and information on C# and .NET development topics • Cyotek](https://www.cyotek.com/blog/getting-a-window-rectangle-without-the-drop-shadow)のスクリーンショット結果に似ているように見えますね。
2
- サイトの方によると、OSのバージョンを調べて、Vista以降なら[DwmGetWindowAttribute](https://msdn.microsoft.com/ja-jp/library/windows/desktop/aa969515(v=vs.85).aspx)を使って[DWMWA_EXTENDED_FRAME_BOUNDS](https://msdn.microsoft.com/ja-jp/library/windows/desktop/aa969530(v=vs.85).aspx#DWMWA_EXTENDED_FRAME_BOUNDS)を取得することでドロップシャドウなしの矩形領域を得られるそうですが、それを試してみるのはいかがでしょう。
2
+ サイトの方によると、OSのバージョンを調べて、Vista以降なら[DwmGetWindowAttribute](https://msdn.microsoft.com/ja-jp/library/windows/desktop/aa969515(v=vs.85).aspx)を使って[DWMWA_EXTENDED_FRAME_BOUNDS](https://msdn.microsoft.com/ja-jp/library/windows/desktop/aa969530(v=vs.85).aspx#DWMWA_EXTENDED_FRAME_BOUNDS)を取得することでドロップシャドウなしの矩形領域を得られるそうですが、それを試してみるのはいかがでしょう。
3
+
4
+ [追記]
5
+ 残念ですね、うまくいきませんでしたか...
6
+ 2012年の投稿ですが、[Bug in DwmGetWindowAttribute DWMWA_EXTENDED_FRAME_BOUNDS
7
+ ](https://social.msdn.microsoft.com/Forums/sqlserver/en-US/3454610b-bac9-41dc-9da9-0ac05cc2e95d/bug-in-dwmgetwindowattribute-dwmwaextendedframebounds?forum=windowsuidevelopment)に寄せられた回答に、Windows 8ではDWMWA_EXTENDED_FRAME_BOUNDSとGetWindowRectが同じになるという報告がありました。バージョンによって挙動がころころ変わるんでしょうかね?
8
+
9
+ ご参考までに、私の環境の場合(Windows 10 バージョン1703)ですと、下記のようにしたところ、いい感じにドロップシャドウなしの画像が得られました。
10
+ ネイティブメソッド関連を別クラスにまとめたためメソッド等の頭に`NativeMethods`が付いていますが、お気になさらないでください。
11
+ `winDC`の原点はGetWindowRectで得られる左上座標っぽかったので、DWMWA_EXTENDED_FRAME_BOUNDSで得た左上座標との差をBitBltのソース座標としましたが、それ以外は特にご質問者さんと違ったことはしていないかと思います。
12
+
13
+ ```C#
14
+ public static Bitmap CaptureActiveWindow()
15
+ {
16
+ //アクティブなウィンドウのデバイスコンテキストを取得
17
+ IntPtr hWnd = NativeMethods.GetForegroundWindow();
18
+ IntPtr winDC = NativeMethods.GetWindowDC(hWnd);
19
+ //ウィンドウの大きさを取得
20
+ NativeMethods.RECT winRect = new NativeMethods.RECT();
21
+ NativeMethods.DwmGetWindowAttribute(
22
+ hWnd,
23
+ NativeMethods.DWMWA_EXTENDED_FRAME_BOUNDS,
24
+ out var bounds,
25
+ Marshal.SizeOf(typeof(NativeMethods.RECT)));
26
+ NativeMethods.GetWindowRect(hWnd, ref winRect);
27
+ //Bitmapの作成
28
+ var offsetX = bounds.left - winRect.left;
29
+ var offsetY = bounds.top - winRect.top;
30
+ Bitmap bmp = new Bitmap(bounds.right - bounds.left,
31
+ bounds.bottom - bounds.top);
32
+ //Graphicsの作成
33
+ Graphics g = Graphics.FromImage(bmp);
34
+ //Graphicsのデバイスコンテキストを取得
35
+ IntPtr hDC = g.GetHdc();
36
+ //Bitmapに画像をコピーする
37
+ int bmpWidth = bmp.Width;
38
+ int bmpHeight = bmp.Height;
39
+ Console.WriteLine(winRect);
40
+ NativeMethods.BitBlt(hDC, 0, 0, bmpWidth, bmpHeight,
41
+ winDC, offsetX, offsetY, NativeMethods.SRCCOPY);
42
+ //解放
43
+ g.ReleaseHdc(hDC);
44
+ g.Dispose();
45
+ NativeMethods.ReleaseDC(hWnd, winDC);
46
+
47
+ return bmp;
48
+ }
49
+ ```

1

列挙値へのリンクを追加

2017/12/03 22:38

投稿

Bongo
Bongo

スコア10818

answer CHANGED
@@ -1,2 +1,2 @@
1
1
  画像を見るに、[Getting a window rectangle without the drop shadow - Articles and information on C# and .NET development topics • Cyotek](https://www.cyotek.com/blog/getting-a-window-rectangle-without-the-drop-shadow)のスクリーンショット結果に似ているように見えますね。
2
- サイトの方によると、OSのバージョンを調べて、Vista以降なら[DwmGetWindowAttribute](https://msdn.microsoft.com/ja-jp/library/windows/desktop/aa969515(v=vs.85).aspx)を使とドロップシャドウなしの矩形領域を求められるそうですが、それを試してみるのはいかがでしょう。
2
+ サイトの方によると、OSのバージョンを調べて、Vista以降なら[DwmGetWindowAttribute](https://msdn.microsoft.com/ja-jp/library/windows/desktop/aa969515(v=vs.85).aspx)を使って[DWMWA_EXTENDED_FRAME_BOUNDS](https://msdn.microsoft.com/ja-jp/library/windows/desktop/aa969530(v=vs.85).aspx#DWMWA_EXTENDED_FRAME_BOUNDS)を取得するこドロップシャドウなしの矩形領域をられるそうですが、それを試してみるのはいかがでしょう。