NEWS

[C#] Chụp ảnh màn hình sử dụng Spy++ Win32 Winform

[C#] Chụp ảnh màn hình sử dụng Spy++ Win32 Winform
Đăng bởi: Thảo Meo - Lượt xem: 5321 16:04:05, 16/09/2020EBOOK

Xin chào các bạn, bài viết hôm nay mình sẻ hướng dẫn cách cách chụp màn hình sử dụng Spy++ Win32 để chụp hình từng đối tượng ứng dụng Windows và hiển thị vào Picturebox.

[C#] Capture Region Screen with Spy++ Win32

Dưới đây là vieo demo sử dụng Spy++:

Các bạn có thể tham khảo bài viết này để viết bài chụp ảnh màn hình Winform.

spy_capture_csharp

Ở hình ảnh demo trên các bạn thấy mình click vào tượng và sử dụng drag and drop mình di chuyển đến từng vị trí của ứng dụng windows sẽ chụp hình.

Nếu bạn nào đã từng sử dụng Auto-IT sẽ biết đến công cụ này.

Source code Capture Screen Region Windows C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SpyWindowCapture
{
    public partial class Form1 : Form
    {
		private bool _capturing;
		private Cursor _cursorDefault;
		private Cursor _cursorFinder;
		private IntPtr _hPreviousWindow;
		public Form1()
        {
            InitializeComponent();
			_pictureBox.Image = Properties.Resources.FinderHome;
			_cursorDefault = Cursor.Current;
			_cursorFinder = GetCursor();
			
		}

		public Cursor GetCursor()
		{
			using (var m = new MemoryStream(Properties.Resources.Finder))
			{
				return new Cursor(m);
			}
		}

		protected override void WndProc(ref Message m)
		{
			switch (m.Msg)
			{				
				case (int)Win32.WindowMessages.WM_LBUTTONUP:
					this.CaptureMouse(false);
					break;				
				case (int)Win32.WindowMessages.WM_MOUSEMOVE:
					this.HandleMouseMovements();
					break;
			};

			base.WndProc(ref m);
		}

		private void HandleMouseMovements()
		{
		
			if (!_capturing)
				return;

			try
			{				
				IntPtr hWnd = Win32.WindowFromPoint(Cursor.Position);				
				if (_hPreviousWindow != IntPtr.Zero && _hPreviousWindow != hWnd)
					WindowHighlighter.Refresh(_hPreviousWindow);

			
				if (hWnd == IntPtr.Zero)
				{				
				}
				else
				{					
					_hPreviousWindow = hWnd;
					Win32.Rect rc = new Win32.Rect();
					Win32.GetWindowRect(hWnd, ref rc);				
					WindowHighlighter.Highlight(hWnd);
					try
					{	
						Image image = GetWindowCaptureAsBitmap(hWnd.ToInt32());
						pictureBox1.Image = image;

					}
					catch (Exception ex)
					{
						Debug.WriteLine(ex);
					}
				}
			}
			catch (Exception ex)
			{
				Debug.WriteLine(ex);
			}
		}

		public static Bitmap GetWindowCaptureAsBitmap(int handle)
		{
			IntPtr hWnd = new IntPtr(handle);
			Win32.Rect rc = new Win32.Rect();
			if (!Win32.GetWindowRect(hWnd, ref rc))
				return null;

			Bitmap bitmap = new Bitmap(rc.Width, rc.Height);
			Graphics gfxBitmap = Graphics.FromImage(bitmap);		
			IntPtr hdcBitmap = gfxBitmap.GetHdc();			
			IntPtr hdcWindow = Win32.GetWindowDC(hWnd);			
			Win32.BitBlt(hdcBitmap, 0, 0, rc.Width, rc.Height, hdcWindow, 0, 0, (int)Win32.TernaryRasterOperations.SRCCOPY);			
			gfxBitmap.ReleaseHdc(hdcBitmap);
			Win32.ReleaseDC(hWnd, hdcWindow);			
			gfxBitmap.Dispose();			
			return bitmap;
		}


		private void _pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                CaptureMouse(true);
        }

		private void CaptureMouse(bool captured)
		{			
			if (captured)
			{				
				Win32.SetCapture(this.Handle);				
				Cursor.Current = _cursorFinder;				
				_pictureBox.Image = Properties.Resources.FinderGone;
			}			
			else
			{				
				Win32.ReleaseCapture();				
				Cursor.Current = _cursorDefault;				
				_pictureBox.Image = Properties.Resources.FinderHome;
				
				if (_hPreviousWindow != IntPtr.Zero)
				{
					WindowHighlighter.Refresh(_hPreviousWindow);
					_hPreviousWindow = IntPtr.Zero;
				}
			}			
			_capturing = captured;
		}
	}
}

Trong project có rất nhiều file nên mình không có up lên ở đây, các bạn download source code về để tham khảo nhé.

Thanks for watching!

DOWNLOAD SOURCE

Tags: spy++ c#capture region windows c#

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Chụp ảnh màn hình sử dụng Spy++ Win32 Winform
Đăng bởi: Thảo Meo - Lượt xem: 5321 16:04:05, 16/09/2020EBOOK