Disable screen capture in windows forms desktop applications using c#
Loading
Disable screen capture in windows forms desktop applications using c#
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jaish MathewsRank #225 · 11K reputationPosted Jan 7, 2025, 7:37 AM EST
To disable screen capture in a Windows Forms desktop application using C#, you can employ techniques such as using window attributes or direct APIs that restrict screen capture.
Below are approaches you can use:
1. Use the
SetWindowDisplayAffinityAPI (Windows 8 and later)The
SetWindowDisplayAffinityfunction prevents screen capture by marking the window as non-screen-capturable. This works for tools like Snipping Tool or Print Screen, but it may not stop all third-party screen recording tools.Implementation
using System; using System.Runtime.InteropServices; using System.Windows.Forms; public class MainForm : Form { // Import the SetWindowDisplayAffinity function [DllImport("user32.dll")] private static extern bool SetWindowDisplayAffinity(IntPtr hWnd, uint affinity); // Constants for display affinity private const uint WDA_NONE = 0; private const uint WDA_MONITOR = 1; protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); // Set the window to prevent screen capture SetWindowDisplayAffinity(this.Handle, WDA_MONITOR); } protected override void OnFormClosed(FormClosedEventArgs e) { // Reset the display affinity when the form is closed SetWindowDisplayAffinity(this.Handle, WDA_NONE); base.OnFormClosed(e); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } }2. Black Out Sensitive Content on Minimized
Instead of preventing screen capture, you can obscure sensitive content when the application is minimized or not in focus.
Implementation
protected override void OnResize(EventArgs e) { base.OnResize(e); if (this.WindowState == FormWindowState.Minimized) { // Hide sensitive content or overlay with black this.Opacity = 0; // Makes the window invisible } else { this.Opacity = 1; // Restore visibility } }3. Block Screen Capture via Registry (Group Policy)
While not a code-level solution, you can modify Windows Group Policies or registry keys to restrict screen capture for specific applications. This requires admin privileges and might not be app-specific.
Limitations
SetWindowDisplayAffinityonly works on systems with DWM (Desktop Window Manager) enabled and might not block all screen capture tools.Best Practices