回答編集履歴

2

追加要件に対応

2016/03/26 07:50

投稿

umed0025
umed0025

スコア851

test CHANGED
@@ -33,3 +33,73 @@
33
33
 
34
34
 
35
35
  注意:フォームに描画されていないものはこの方法でとれなかったはず。
36
+
37
+
38
+
39
+ 追記
40
+
41
+ ===
42
+
43
+ 要件が追加されたので違うサンプルを。gdi32 BitBltを使う方法になります。
44
+
45
+ PrintWindowあたりでもいーんですが取れないことがあるんでこっちで。
46
+
47
+ [BitBlt](http://www.pinvoke.net/default.aspx/gdi32/BitBlt.html)
48
+
49
+ [PrintWindow](http://www.pinvoke.net/default.aspx/user32.printwindow)
50
+
51
+ ```C#
52
+
53
+ //using System.Drawing;
54
+
55
+ //using System.Windows.Forms;
56
+
57
+ //using System.Runtime.InteropServices;
58
+
59
+ [DllImport("gdi32.dll")]
60
+
61
+ private static extern int BitBlt(IntPtr hDestDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);
62
+
63
+ public static Bitmap CaptureControl(Control target)
64
+
65
+ {
66
+
67
+ Bitmap caputuredBitmap;
68
+
69
+ using (Graphics targetGraphic = target.CreateGraphics())
70
+
71
+ {
72
+
73
+ caputuredBitmap = new Bitmap(target.Width, target.Height);
74
+
75
+ using (Graphics capturedGraphics = Graphics.FromImage(caputuredBitmap))
76
+
77
+ {
78
+
79
+ IntPtr targetDc = targetGraphic.GetHdc();
80
+
81
+ IntPtr capturedDc = capturedGraphics.GetHdc();
82
+
83
+ BitBlt(capturedDc, 0, 0, target.Width, target.Height, targetDc, 0, 0, 0xCC0020);
84
+
85
+ targetGraphic.ReleaseHdc(targetDc);
86
+
87
+ capturedGraphics.ReleaseHdc(capturedDc);
88
+
89
+ }
90
+
91
+ }
92
+
93
+
94
+
95
+ return caputuredBitmap;
96
+
97
+ }
98
+
99
+ ```
100
+
101
+
102
+
103
+
104
+
105
+

1

誤字修正

2016/03/26 07:50

投稿

umed0025
umed0025

スコア851

test CHANGED
@@ -1,6 +1,6 @@
1
1
  フォーム上で描写されているRichTextBoxのイメージ取る形でよければこんな感じでしょうか?
2
2
 
3
- やってることは、PictureBox(Control)の座標を元にキャプチャしてるだけです。
3
+ やってることは、RichTextBox(Control)の座標を元にキャプチャしてるだけです。
4
4
 
5
5
  PictureBoxを指定すれば、RichTextBoxを埋め込んだ感じのイメージがとれるかと。
6
6