■ 바탕 화면에서 애니메이션 이미지를 출력하는 방법을 보여준다.
▶ WIN32APIHelper.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 |
using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; namespace TestProject { public static class WIN32Helper { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region DC 구하기 - GetDC(windowHandle) /// <summary> /// DC 구하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <returns>디바이스 컨텍스트 핸들</returns> [DllImport("user32", ExactSpelling = true, SetLastError = true)] public static extern IntPtr GetDC(IntPtr windowHandle); #endregion #region DC 해제하기 - ReleaseDC(windowHandle, deviceContextHandle) /// <summary> /// DC 해제하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="deviceContextHandle">디바이스 컨텍스트 핸들</param> /// <returns>처리 결과</returns> [DllImport("user32", ExactSpelling = true)] public static extern IntPtr ReleaseDC(IntPtr windowHandle, IntPtr deviceContextHandle); #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 데스크톱 윈도우 구하기 - GetDesktopWindow() /// <summary> /// 데스크톱 윈도우 구하기 /// </summary> /// <returns>데스크톱 윈도우 핸들</returns> [DllImport("user32", EntryPoint = "GetDesktopWindow")] private static extern IntPtr GetDesktopWindow(); #endregion #region 비트맵 복사하기 - BitBlt(targetDCHandle, targetX, targetY, width, height, sourceDCHandle, sourceX, sourceY, rasterOperationCode /// <summary> /// 비트맵 복사하기 /// </summary> /// <param name="targetDeviceContextHandle">타겟 디바이스 컨텍스트 핸들</param> /// <param name="targetX">타겟 X</param> /// <param name="targetY">타겟 Y</param> /// <param name="width">너비</param> /// <param name="height">높이</param> /// <param name="sourceDeviceContextHandle">소스 디바이스 컨텍스트 핸들</param> /// <param name="sourceX">소스 X</param> /// <param name="sourceY">소스 Y</param> /// <param name="rasterOperationCode">래스터 연산 코드</param> /// <returns>처리 결과</returns> [DllImport("gdi32", ExactSpelling = true)] private static extern IntPtr BitBlt ( IntPtr targetDCHandle, int targetX, int targetY, int width, int height, IntPtr sourceDCHandle, int sourceX, int sourceY, int rasterOperationCode ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 화면 캡처하기 - CaptureScreen() /// <summary> /// 화면 캡처하기 /// </summary> /// <returns>화면 캡처 비트맵</returns> public static Bitmap CaptureScreen() { int screenWidth = Screen.PrimaryScreen.Bounds.Width; int screenHeight = Screen.PrimaryScreen.Bounds.Height; Bitmap screenBitmap = new Bitmap(screenWidth, screenHeight); Graphics screenGraphics = Graphics.FromImage(screenBitmap); IntPtr desktopDeviceContextHandle = GetDC(GetDesktopWindow()); IntPtr screenDeviceContextHandle = screenGraphics.GetHdc(); BitBlt ( screenDeviceContextHandle, 0, 0, screenWidth, screenHeight, desktopDeviceContextHandle, 0, 0, 0x00CC0020 // SRCCOPY ); ReleaseDC(GetDesktopWindow(), desktopDeviceContextHandle); screenGraphics.ReleaseHdc(screenDeviceContextHandle); screenGraphics.Dispose(); return screenBitmap; } #endregion } } |
▶ 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 |
using System; using System.Drawing; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.moveButton.Click += moveButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 이동 버튼 클릭시 처리하기 - moveButton_Click(sender, e) /// <summary> /// 이동 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void moveButton_Click(object sender, EventArgs e) { const int MOVE_STEP = 10; Bitmap bitmap = new Bitmap(Image.FromFile("IMAGE\\sample.png")); Rectangle targetRectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height); Bitmap desktopBitmap = WIN32Helper.CaptureScreen(); Bitmap bufferBitmap = new Bitmap(targetRectangle.Width + MOVE_STEP, targetRectangle.Height + MOVE_STEP); IntPtr desktopDCHandle = WIN32Helper.GetDC(IntPtr.Zero); Graphics desktopGraphics = Graphics.FromHdc(desktopDCHandle); Graphics bufferGraphics = Graphics.FromImage(bufferBitmap); Point targetPoint = new Point(500, 300); for(int i = 0, j = 0; i < 100; i += MOVE_STEP, j += MOVE_STEP) { bufferGraphics.DrawImage ( desktopBitmap, 0, 0, new Rectangle ( targetPoint.X + i, targetPoint.Y + j, targetRectangle.Width + MOVE_STEP, targetRectangle.Height + MOVE_STEP ), GraphicsUnit.Pixel ); bufferGraphics.DrawImage ( bitmap, MOVE_STEP, MOVE_STEP, targetRectangle, GraphicsUnit.Pixel ); desktopGraphics.DrawImage(bufferBitmap, targetPoint.X + i, targetPoint.Y + j); } for(int i = 100, j = 100; i > 0; i -= MOVE_STEP, j -= MOVE_STEP) { bufferGraphics.DrawImage ( desktopBitmap, 0, 0, new Rectangle ( targetPoint.X + i, targetPoint.Y + j, targetRectangle.Width + MOVE_STEP, targetRectangle.Height + MOVE_STEP ), GraphicsUnit.Pixel ); bufferGraphics.DrawImage(bitmap, 0, 0, targetRectangle, GraphicsUnit.Pixel); desktopGraphics.DrawImage(bufferBitmap, targetPoint.X + i, targetPoint.Y + j); } bufferGraphics.Dispose(); desktopGraphics.Dispose(); WIN32Helper.ReleaseDC(IntPtr.Zero, desktopDCHandle); } #endregion } } |