■ Graphics 클래스의 CopyFromScreen 메소드를 사용해 화면을 캡처하는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
using System; using System.Drawing; using System.Drawing.Imaging; using System.Media; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 사운드 플레이어 /// </summary> private SoundPlayer soundPlayer = new SoundPlayer(); /// <summary> /// 위치 설정 가능 여부 /// </summary> bool canSetLocation = true; /// <summary> /// 폼 위치 /// </summary> private Point formLocation; /// <summary> /// 폼 크기 /// </summary> private Size formSize; /// <summary> /// 이미지 저장 가능 여부 /// </summary> bool canSaveImage = false; /// <summary> /// 그래픽스 /// </summary> private Graphics graphics = null; /// <summary> /// 비트맵 /// </summary> private Bitmap bitmap = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); LocationChanged += Form_LocationChanged; KeyPress += Form_KeyPress; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 폼 위치 변경시 처리하기 - Form_LocationChanged(sender, e) /// <summary> /// 폼 위치 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_LocationChanged(object sender, EventArgs e) { if(this.canSetLocation == true) { this.formLocation = Location; this.formSize = Size; } } #endregion #region 폼 키 PRESS시 처리하기 - Form_KeyPress(sender, e) /// <summary> /// 폼 키 PRESS시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_KeyPress(object sender, KeyPressEventArgs e) { if(e.KeyChar == 'c' || e.KeyChar == 'C') { this.canSetLocation = false; this.canSaveImage = true; FormBorderStyle = FormBorderStyle.None; Location = new Point(0, 0); Size = Screen.PrimaryScreen.Bounds.Size; Opacity = 0.0; Rectangle rectangle = Screen.PrimaryScreen.Bounds; this.bitmap = new Bitmap(rectangle.Width, rectangle.Height); this.graphics = Graphics.FromImage(this.bitmap); this.graphics.CopyFromScreen(PointToScreen(new Point(0,0)), new Point(0, 0), rectangle.Size); this.pictureBox.Image = this.bitmap; this.soundPlayer.SoundLocation = @"..\..\wav\capture.wav"; this.soundPlayer.Play(); FormBorderStyle = FormBorderStyle.FixedSingle; Location = formLocation; Size = formSize; Opacity = 100.0; this.canSetLocation = true; } else if(e.KeyChar == 'e' || e.KeyChar == 'E') { this.soundPlayer.SoundLocation = @"..\..\wav\ereser.wav"; this.soundPlayer.Play(); this.canSaveImage = false; this.pictureBox.Image = null; } else if(e.KeyChar == 's' || e.KeyChar == 'S') { if(this.canSaveImage == true) { using(SaveFileDialog saveFileDialog = new SaveFileDialog()) { saveFileDialog.OverwritePrompt = true; saveFileDialog.FileName = "capture"; saveFileDialog.Filter = "Image file(*.png)|*.png"; DialogResult dialogResult = saveFileDialog.ShowDialog(); if(dialogResult == DialogResult.OK) { this.bitmap.Save(saveFileDialog.FileName, ImageFormat.Png); } } } else { MessageBox.Show("캡처한 화면이 없습니다.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } #endregion } } |