回答編集履歴

2

実験したコードを追記

2017/12/03 22:38

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -1,3 +1,97 @@
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
2
 
3
3
  サイトの方によると、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)を取得することでドロップシャドウなしの矩形領域を得られるそうですが、それを試してみるのはいかがでしょう。
4
+
5
+
6
+
7
+ [追記]
8
+
9
+ 残念ですね、うまくいきませんでしたか...
10
+
11
+ 2012年の投稿ですが、[Bug in DwmGetWindowAttribute DWMWA_EXTENDED_FRAME_BOUNDS
12
+
13
+ ](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が同じになるという報告がありました。バージョンによって挙動がころころ変わるんでしょうかね?
14
+
15
+
16
+
17
+ ご参考までに、私の環境の場合(Windows 10 バージョン1703)ですと、下記のようにしたところ、いい感じにドロップシャドウなしの画像が得られました。
18
+
19
+ ネイティブメソッド関連を別クラスにまとめたためメソッド等の頭に`NativeMethods`が付いていますが、お気になさらないでください。
20
+
21
+ `winDC`の原点はGetWindowRectで得られる左上座標っぽかったので、DWMWA_EXTENDED_FRAME_BOUNDSで得た左上座標との差をBitBltのソース座標としましたが、それ以外は特にご質問者さんと違ったことはしていないかと思います。
22
+
23
+
24
+
25
+ ```C#
26
+
27
+ public static Bitmap CaptureActiveWindow()
28
+
29
+ {
30
+
31
+ //アクティブなウィンドウのデバイスコンテキストを取得
32
+
33
+ IntPtr hWnd = NativeMethods.GetForegroundWindow();
34
+
35
+ IntPtr winDC = NativeMethods.GetWindowDC(hWnd);
36
+
37
+ //ウィンドウの大きさを取得
38
+
39
+ NativeMethods.RECT winRect = new NativeMethods.RECT();
40
+
41
+ NativeMethods.DwmGetWindowAttribute(
42
+
43
+ hWnd,
44
+
45
+ NativeMethods.DWMWA_EXTENDED_FRAME_BOUNDS,
46
+
47
+ out var bounds,
48
+
49
+ Marshal.SizeOf(typeof(NativeMethods.RECT)));
50
+
51
+ NativeMethods.GetWindowRect(hWnd, ref winRect);
52
+
53
+ //Bitmapの作成
54
+
55
+ var offsetX = bounds.left - winRect.left;
56
+
57
+ var offsetY = bounds.top - winRect.top;
58
+
59
+ Bitmap bmp = new Bitmap(bounds.right - bounds.left,
60
+
61
+ bounds.bottom - bounds.top);
62
+
63
+ //Graphicsの作成
64
+
65
+ Graphics g = Graphics.FromImage(bmp);
66
+
67
+ //Graphicsのデバイスコンテキストを取得
68
+
69
+ IntPtr hDC = g.GetHdc();
70
+
71
+ //Bitmapに画像をコピーする
72
+
73
+ int bmpWidth = bmp.Width;
74
+
75
+ int bmpHeight = bmp.Height;
76
+
77
+ Console.WriteLine(winRect);
78
+
79
+ NativeMethods.BitBlt(hDC, 0, 0, bmpWidth, bmpHeight,
80
+
81
+ winDC, offsetX, offsetY, NativeMethods.SRCCOPY);
82
+
83
+ //解放
84
+
85
+ g.ReleaseHdc(hDC);
86
+
87
+ g.Dispose();
88
+
89
+ NativeMethods.ReleaseDC(hWnd, winDC);
90
+
91
+
92
+
93
+ return bmp;
94
+
95
+ }
96
+
97
+ ```

1

列挙値へのリンクを追加

2017/12/03 22:38

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -1,3 +1,3 @@
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
2
 
3
- サイトの方によると、OSのバージョンを調べて、Vista以降なら[DwmGetWindowAttribute](https://msdn.microsoft.com/ja-jp/library/windows/desktop/aa969515(v=vs.85).aspx)を使とドロップシャドウなしの矩形領域を求められるそうですが、それを試してみるのはいかがでしょう。
3
+ サイトの方によると、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)を取得するこドロップシャドウなしの矩形領域をられるそうですが、それを試してみるのはいかがでしょう。