Sample code sharing for C # simulation Printscreen and Alt+printscreen capturing screen pictures

Source: Internet
Author: User
C # simulates Printscreen and Alt+printscreen capturing screen pictures

Keybd_event API

function function: This function synthesizes a keystroke event. The system can use this synthetic keystroke event to generate a wm_keyup or WM_KEYDOWN message, and the keyboard driver's interrupt handler calls the Keybd_event function. In Windows NT, the function has already been replaced with SendInput.

function prototype; VOID keybd_event (BYTE bvk,byte bscan,dword dwflags,dword dwextralnfo);

Parameters:

BVK: Defines a virtual key code. The key code value must be between 1~254.
Bscan: Defines the hardware scan code for this key.
DwFlags: Defines a set of flags for each aspect of a function operation. The application can set the flag bit using the combination of some of the predefined constants below.
Keyeventf_extendedkey: If this value is specified, a prefix byte with a value of Oxeo (224) is preceded by the scan code.
Keyeventf_keyup: If the value is specified, the key is freed, and if the value is not specified, the key is pressed.
Dwextralnfo: Defines the additional 32-bit value associated with the keystroke.
Return value: The function has no return value.

Full code:

Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.text;using system.windows.forms;using system.runtime.interopservices;namespace PrintScreen{Public            Partial class Form1:form {[DllImport ("User32.dll")] static extern void keybd_event ( BYTE bvk,//virtual key value byte bscan,//hardware scan code UINT dwflags,//action ID IntPtr dwextrainfo//with keyboard        Auxiliary information for action correlation);        <summary>///Analog Print Screen Keyboard messages, capturing full-size images. </summary> public void Printscreen () {keybd_event ((byte) 0x2c, 0, 0x0, IntPtr.Zero);//d             Own application.doevents ();         Keybd_event ((byte) 0x2c, 0, 0x2, IntPtr.Zero);//up application.doevents ();        }///<summary>///Analog ALT Print Screen Keyboard message, capturing the current window picture. </summary> public void Altprintscreen () {keybd_event((byte) keys.menu, 0, 0x0, IntPtr.Zero);            Keybd_event ((byte) 0x2c, 0, 0x0, IntPtr.Zero);//down application.doevents ();            Application.doevents ();            Keybd_event ((byte) 0x2c, 0, 0x2, IntPtr.Zero);//up keybd_event ((byte) keys.menu, 0, 0x2, IntPtr.Zero);            Application.doevents ();        Application.doevents ();        } public Form1 () {InitializeComponent (); private void Form1_Load (object sender, EventArgs e) {}///<summary>//from clipboard             Take pictures///</summary>//<returns></returns> private Bitmap getscreenimage () {            IDataObject newobject = null;            Bitmap newbitmap = null;                try {application.doevents ();                NewObject = Clipboard.getdataobject (); if (Clipboard.containsimage ()) {Newbitmap = (Bitmap) (ClipboArd. GetImage ().                Clone ());            } return Newbitmap; } catch (Exception ex) {Console.WriteLine (ex.                Message);            return null; }} private void Button1_Click (object sender, EventArgs e) {button1.            Enabled = false;            pictureBox1.Image = null;            Printscreen ();            pictureBox1.Image = Getscreenimage (); Button1.            Enabled = true;        Application.doevents (); The private void button2_click (object sender, EventArgs e) {button2.            Enabled = false;            pictureBox1.Image = null;            Altprintscreen ();            pictureBox1.Image = Getscreenimage (); Button2.            Enabled = true;        Application.doevents (); }    }}

Operating effect:


Legacy issues:

Printscreen does not have a task problem, but the first time to use Altprintscreen is always unable to get the correct picture, do not know why! I hope the expert passed the guidance twos, younger brother greatly appreciated!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.