Use C # To create a screen capture program (4)

Source: Internet
Author: User

Use C # To create a screen capture program (4)

Complete code:

Using System;

Using System. Drawing;

Using System. Collections;

Using System. ComponentModel;

Using System. Windows. Forms;

Using System. Data;

Using System. Drawing. Imaging;


Namespace FormCapture

{

///

/// Summary description for Form1.

///

Public class Form1: System. Windows. Forms. Form

{

Private System. Windows. Forms. PictureBox pictureBox1;

Private System. Windows. Forms. Button button1;

///

/// Required designer variable.

///

Private System. ComponentModel. Container components = null;

 

Public Form1 ()

{

//

// Required for Windows Form Designer support

//

InitializeComponent ();


//

// TODO: Add any constructor code after InitializeComponent call

//

}


///

/// Clean up any resources being used.

///

Protected override void Dispose (bool disposing)

{

If (disposing)

{

If (components! = Null)

{

Components. Dispose ();

}

}

Base. Dispose (disposing );

}

 

# Region Windows Form Designer generated code

///

/// Required method for Designer support-do not modify

/// The contents of this method with the code editor.

///

Private void InitializeComponent ()

{

System. Resources. ResourceManager resources = new System. Resources. ResourceManager (typeof (Form1 ));

This. button1 = new System. Windows. Forms. Button ();

This. pictureBox1 = new System. Windows. Forms. PictureBox ();

This. SuspendLayout ();

//

// Button1

//

This. button1.BackColor = System. Drawing. SystemColors. ActiveBorder;

This. button1.ForeColor = System. Drawing. SystemColors. ControlDarkDark;

This. button1.Location = new System. Drawing. Point (272, 19 );

This. button1.Name = "button1 ";

This. button1.Size = new System. Drawing. Size (72, 27 );

This. button1.TabIndex = 4;

This. button1.Text = "screen capture ";

This. button1.Click + = new System. EventHandler (this. button#click );

//

// PictureBox1

//

This. pictureBox1.Image = (System. Drawing. Bitmap) (resources. GetObject ("pictureBox1.Image ")));

This. pictureBox1.Location = new System. Drawing. Point (16, 16 );

This. pictureBox1.Name = "pictureBox1 ";

This. pictureBox1.Size = new System. Drawing. Size (240,224 );

This. pictureBox1.SizeMode = System. Windows. Forms. PictureBoxSizeMode. StretchImage;

This. pictureBox1.TabIndex = 0;

This. pictureBox1.TabStop = false;

//

// Form1

//

This. AutoScaleBaseSize = new System. Drawing. Size (6, 14 );

This. ClientSize = new System. Drawing. Size (358,255 );

This. Controls. AddRange (new System. Windows. Forms. Control [] {

This. button1,

This. pictureBox1 });

This. KeyPreview = true;

This. Name = "Form1 ";

This. Text = "screen capture program ";

This. ResumeLayout (false );

 

}

# Endregion

 

[System. Runtime. InteropServices. DllImportAttribute ("gdi32.dll")]

Private static extern bool BitBlt (

IntPtr hdcDest,File: // objectDC handle

Int nXDest,File: // objectX coordinate in the upper left corner of the graph

Int nYDest,File: // objectY coordinate in the upper left corner of the graph

Int nWidth,File: // objectThe width of the rectangle in the image.

Int nHeight,File: // objectThe rectangle height of the image.

IntPtr hdcSrc,File: // SourceDC handle

Int nXSrc,File: // SourceX coordinate in the upper left corner of the graph

Int nYSrc,File: // SourceX coordinate in the upper left corner of the graph

System. Int32 dwdropFile: // opticalGrid Operation Code

);

 

Private void button#click (object sender, System. EventArgs e)

{

Graphics g1 = this. CreateGraphics (); // obtain the form image object

Image MyImage = new Bitmap (this. ClientRectangle. Width, this. ClientRectangle. Height, g1 );

Graphics g2 = Graphics. FromImage (MyImage); // create a bitmap image object

IntPtr dc1 = g1.GetHdc (); // obtain the context device of the form

IntPtr dc2 = g2.GetHdc (); // context device for obtaining bitmap files

BitBlt (dc2, 0, 0, this. ClientRectangle. Width, this. ClientRectangle. Height, dc1, 0, 0, 13369376); // write in-place Graph

G1.ReleaseHdc (dc1); // release the context device of the form

G2.ReleaseHdc (dc2); // The context device that releases the bitmap file.

MyImage. Save (@ "c: Captured.jpg", ImageFormat. Jpeg); // Save as a jpeg file

MessageBox. Show ("saving the image ends! ");

}

 

///

/// The main entry point for the application.

///

[STAThread]

Static void Main ()

{

Application. Run (new Form1 ());

}

}

}

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.