C# 如何防止打印屏幕

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/448106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:44:19  来源:igfitidea点击:

How do I prevent print screen

c#.netwinforms.net-2.0c#-2.0

提问by

I have a requirement that an application I am working on prevent the user from being able to easily capture the contents of the screen.

我要求我正在处理的应用程序阻止用户轻松捕获屏幕内容。

I have communicated that there is no feasible way to completely prevent this from happening, but I'm looking for methods to introduce some hurdles to the process.

我已经表示没有可行的方法可以完全防止这种情况发生,但我正在寻找方法来为该过程引入一些障碍。

I'm using C#/.NET 2.0 and WinForms

我正在使用 C#/.NET 2.0 和 WinForms

回答by Andrei Krotkov

Well, you could try capturing the button, but I'm not sure how well that will work.

好吧,您可以尝试捕获按钮,但我不确定它的效果如何。

One thing that always annoyed me was that whenever I played a movie, it would never take screenshots of it. If you can render through a separate context, it would make it really annoying to take a picture of it. Perhaps you can send your screen output through something like that?

一直让我烦恼的一件事是,每当我播放一部电影时,它都不会截屏。如果您可以通过单独的上下文进行渲染,那么拍摄它会非常烦人。也许您可以通过类似的方式发送屏幕输出?

回答by z -

From here:

这里:

A. Windows implements Print Screen using a registered hotkey. Windows uses the predefined hotkeys IDHOT_SNAPDESKTOP and IDHOT_SNAPWINDOW to handle Print Screen. These correspond to Print Screen, which captures the entire screen, and Alt+Print Screen, which captures only the active window. To disable these functions all you have to do is register the hotkeys, which causes Windows to send your app a WM_HOTKEY message when the user presses either hotkey. Your implementation can ignore the message to bypass the default screen-capture behavior. A good place to do it is in your mainframe class.

A. Windows 使用注册的热键实现 Print Screen。Windows 使用预定义的热键 IDHOT_SNAPDESKTOP 和 IDHOT_SNAPWINDOW 来处理打印屏幕。它们对应于 Print Screen(捕获整个屏幕)和 Alt+Print Screen(仅捕获活动窗口)。要禁用这些功能,您只需注册热键,这会导致 Windows 在用户按下任一热键时向您的应用发送 WM_HOTKEY 消息。您的实现可以忽略该消息以绕过默认的屏幕捕获行为。一个很好的地方是在你的大型机类中。

回答by Michael Todd

You'll have two cases here that you need to worry about. One, when your window/application has focus, the other when it doesn't have focus.

您将在这里需要担心两种情况。一种,当您的窗口/应用程序有焦点时,另一种是当它没有焦点时。

When it doesn't have focus, there's not a whole lot you can do, i.e. if the user clicks off of your app and onto the desktop, keys aren't sent to your app so you'll never see them. In that case, you can minimize to the tray when your app loses focus (or, perhaps, place a "blank" panel over the form to prevent users from seeing anything on it which will also prevent a print-screen from being useful).

当它没有焦点时,您无能为力,即如果用户点击您的应用程序并点击桌面,密钥不会发送到您的应用程序,因此您永远不会看到它们。在这种情况下,您可以在您的应用程序失去焦点时最小化到托盘(或者,可能在表单上放置一个“空白”面板以防止用户看到它上面的任何内容,这也会阻止打印屏幕有用)。

In the other case, when you have focus, capture keystrokes and examine them. If the Alt key is down and the PrintScreen key is down, reset the value so that a print-screen doesn't occur. (Come to think of it, that may not work. I'd need to test it to be sure.)

在另一种情况下,当你有焦点时,捕捉击键并检查它们。如果按下 Alt 键且按下 PrintScreen 键,请重置该值,以免出现打印屏幕。(想想看,这可能行不通。我需要测试一下才能确定。)

回答by Jeff Yates

You could look into what movie players do. I believe they render directly to a hardware surface (via DirectX). I suspect that you'd need to do this.

你可以看看电影播放器​​是做什么的。我相信它们直接渲染到硬件表面(通过 DirectX)。我怀疑你需要这样做。

回答by FryGuy

You can't.

你不能。

The best you can do is render to a hardware accelerated device on an overlay, similar to what video players used to do. Basically, you paint your entire window blue, and render your graphics onto the video card, and internally the video card will replace the blue with the graphics. The downside to this is you have to give up using winforms controls, and I don't know of any way to do this with .NET easily. I think if you use DirectShow.NET, one of their samples is putting your own graphics into a stream.

您能做的最好的事情是渲染到覆盖层上的硬件加速设备,类似于视频播放器过去所做的。基本上,您将整个窗口涂成蓝色,然后将您的图形渲染到视频卡上,而在内部,视频卡将用图形替换蓝色。这样做的缺点是你必须放弃使用 winforms 控件,我不知道有什么方法可以轻松地使用 .NET 做到这一点。我认为如果您使用DirectShow.NET,他们的示例之一就是将您自己的图形放入流中。

Even after doing all of that, it's still possible to get a screenshot. Just take a picture of the screen with a digital camera.

即使在完成所有这些之后,仍然可以获取屏幕截图。只需用数码相机拍摄屏幕照片即可。

回答by scottm

FWIW, it is possible. Here's some code:

FWIW,这是可能的。这是一些代码:

This would be a dll that you create, then call the HookKeyboard method from your application. I've tested it and it works. Granted, if someone takes a picture with a camera it can't help, but, point made. NYAH!

这将是您创建的 dll,然后从您的应用程序调用 HookKeyboard 方法。我已经测试过它并且它有效。当然,如果有人用相机拍照,那是无济于事的,但是,指出。尼亚!

    namespace KeyboardHook
    {
        public class Hooker
        {

            [StructLayout(LayoutKind.Sequential)]
            public struct KBDLLHOOKSTRUCT
            {
                public int vkCode;
                public int scanCode;
                public int flags;
                public int time

;
            public int extraInfo;
        }

        public delegate int HookProc(int nCode, int wParam, IntPtr ptrKBDLLHOOKSTRUCT);


        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        public static extern IntPtr SetWindowsHookEx(int idHook, HookProc callBack, IntPtr hMod, int threadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        public static extern int CallNextHookEx(IntPtr hhk, int nCode, int wParam, IntPtr lParam);

        private static IntPtr kbh_Handle;
        private static HookProc kbh_HookProc;

        private const int VK_SNAPSHOT = 0x2C;
        private const int WM_KEYDOWN = 0x0100;
        private const int WM_SYSKEYDOWN = 0x0104;
        private const int WH_KEYBOARD_LL = 13;

        private static int LowLevelKeyboardProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode < 0)
            {
                CallNextHookEx(kbh_Handle, nCode, wParam, lParam);
                return 0;
            }

            if (wParam == WM_KEYDOWN)
            {
                IntPtr kbdll = lParam;
                KBDLLHOOKSTRUCT kbdllstruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(kbdll, typeof(KBDLLHOOKSTRUCT));

                if (kbdllstruct.vkCode == VK_SNAPSHOT)
                    return -1;

            }

            return CallNextHookEx(kbh_Handle, nCode, wParam, lParam);
        }

        public static void HookKeyboard()
        {
            try
            {
                kbh_HookProc = LowLevelKeyboardProc;

                kbh_Handle = SetWindowsHookEx(WH_KEYBOARD_LL, kbh_HookProc, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);

                if (kbh_Handle != IntPtr.Zero)
                    System.Diagnostics.Debug.WriteLine(String.Format("It worked! HookHandle: {0}", kbh_Handle));
                else
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(String.Format("ERROR: {0}", ex.Message));
            }
        }
    }
}

回答by Aif

This doesn't really answer the questions, but keep in mind that there exists tools to capture screen, and that a simple camera breaks everything.

这并没有真正回答问题,但请记住,存在捕捉屏幕的工具,而一个简单的相机会打破一切。

I mean ok you "have to", but I would (but I'm young and still student, so I don't know much about what can be said) answer that this is just stupid.

我的意思是好吧,你“必须”,但我会(但我还年轻,仍然是学生,所以我对可以说的内容知之甚少)回答说这只是愚蠢的。

回答by Aif

There are applications that can capture the screen from OpenGL and DirectX apps ! (depending (they are used for recording game movies) ps. windows aero is DirectX

有些应用程序可以从 OpenGL 和 DirectX 应用程序捕获屏幕!(取决于(它们用于录制游戏电影)ps. windows aero 是 DirectX

http://www.fraps.com/i think thats the application

http://www.fraps.com/我认为这就是应用程序

回答by Mat

Check out the new tech - sivizion.com, they prevent print screen all together - no way to bypass it. If anyone will figure out a way how to hack it, please post here, I couldn't. I think they also license their tech, not sure, check it out.

查看新技术 - sivizion.com,它们一起防止打印屏幕 - 无法绕过它。如果有人想办法破解它,请在这里发帖,我不能。我认为他们也许可他们的技术,不确定,检查一下。

回答by Eric Grange

You can make any casual Print Screen useless using Visual Cryptography and taking advantage of retinal persistence (see this articlefor details, and bit.ly/vcryptofor a web demo).

您可以使用 Visual Cryptography 和利用视网膜持久性使任何随意的 Print Screen 变得无用(有关详细信息,请参阅此文章,有关web 演示的bit.ly/vcrypto)。

The idea is to alternate at high frequency between two or more random noise images, that will combine through persistence of vision to reveal the content. A screen capture will only grab one image, with meaningless random noise.

这个想法是在两个或多个随机噪声图像之间以高频交替,这将通过视觉暂留组合来揭示内容。屏幕截图只会抓取一张图像,带有无意义的随机噪声。

This comes at the cost of flickering and inducing user headaches, can be defeated by a camera taking a picture of the screen, or by a less casual user that knows photoshop, but will defeat any kind of casual screen capture or frame grabbing.

这是以闪烁和引起用户头疼的代价为代价的,可能会被相机拍摄屏幕照片所打败,或者被一个了解 Photoshop 的不那么随意的用户打败,但会打败任何形式的随意屏幕捕获或帧抓取。

Might occasionally be useful, in an academic meaning of the term!

有时可能有用,在该术语的学术意义中!