- [DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#
- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
[C#] Chụp ảnh màn hình sử dụng Spy++ Win32 Winform
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.
Ở 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!