Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C # to do Screen Capture Program

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use C # to do Screen Capture program", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "how to use C # to do Screen Capture program" bar!

After mastering some C # source code, you can get the source code (Capture.cs) of using C # to do Screen Capture programs, as follows:

Using System

Using System.Drawing

Using System.Collections

Using System.ComponentModel

Using System.Windows.Forms

Using System.Data

Using System.Drawing.Imaging

Using System.IO

/ / Import the namespaces used in the program

Public class Capture: Form

{

Private System.ComponentModel.Container components = null

Private Icon mNetTrayIcon = new Icon ("Tray.ico")

Private Bitmap MyImage = null

Private NotifyIcon TrayIcon

Private ContextMenu notifyiconMnu

Public Capture ()

{

/ / initialize the components used in the form

InitializeComponent ()

}

Protected override void OnActivated (EventArgs e)

{

This.Hide ()

}

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

Private static extern bool BitBlt (

IntPtr hdcDest, / / handle to the target device

Int nXDest, / / the X coordinate of the upper left corner of the target object

Int nYDest, / / the X coordinate of the upper left corner of the target object

Int nWidth, / / the width of the rectangle of the target object

Int nHeight, / / the length of the rectangle of the target object

IntPtr hdcSrc, / / handle to the source device

Int nXSrc, / / the X coordinate of the upper left corner of the source object

Int nYSrc, / / the X coordinate of the upper left corner of the source object

Operation value of System.Int32 dwRop / / raster

)

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

Private static extern IntPtr CreateDC (

String lpszDriver, / / driver name

String lpszDevice, / / device name

String lpszOutput, / / useless. You can set the location "NULL".

IntPtr lpInitData / / any printer data

)

Public void capture (object sender, System.EventArgs e)

{

This.Visible = false

IntPtr dc1 = CreateDC ("DISPLAY", null, null, (IntPtr) null)

/ / create the DC of the monitor

Graphics G1 = Graphics.FromHdc (dc1)

/ / create a new Graphics object from a handle to the specified device

MyImage = new Bitmap (Screen.PrimaryScreen.Bounds.Width

Screen.PrimaryScreen.Bounds.Height, G1)

/ / create a Bitmap object of the same size according to the screen size

Graphics G2 = Graphics.FromImage (MyImage)

/ / get the handle to the screen

IntPtr dc3 = g1.GetHdc ()

/ / get the handle to the bitmap

IntPtr dc2 = g2.GetHdc ()

/ / capture the current screen to the bitmap object

BitBlt (dc2, 0,0, Screen.PrimaryScreen.Bounds.Width

Screen.PrimaryScreen.Bounds.Height, dc3, 0,0,13369376)

/ / copy the current screen into the picture

G1.ReleaseHdc (dc3)

/ / release the screen handle

G2.ReleaseHdc (dc2)

/ / release bitmap handle

MyImage.Save ("c:\\ MyJpeg.jpg", ImageFormat.Jpeg)

MessageBox.Show ("saved the current screen to the C:\\ MyJpeg.jpg file!" )

This.Visible = true

}

Public void ExitSelect (object sender, System.EventArgs e)

{

/ / hide the icon in the tray program

TrayIcon.Visible = false

/ / shut down the system

This.Close ()

}

/ / clear the resources used in the program

Public override void Dispose ()

{

Base.Dispose ()

If (components! = null)

Components.Dispose ()

}

Private void InitializeComponent ()

{

/ / set the properties of the pallet program

TrayIcon = new NotifyIcon ()

TrayIcon.Icon = mNetTrayIcon

TrayIcon.Text = "use C # to do Screen Capture programs"

TrayIcon.Visible = true

/ / define a MenuItem array and assign this array to the ContextMenu object at the same time

MenuItem [] mnuItms = new MenuItem [3]

MnuItms [0] = new MenuItem ()

MnuItms [0] .Text = "capture the current screen!"

MnuItms [0] .Click + = new System.EventHandler (this.capture)

MnuItms [1] = new MenuItem ("-")

MnuItms [2] = new MenuItem ()

MnuItms [2] .Text = "exit the system"

MnuItms [2] .Click + = new System.EventHandler (this.ExitSelect)

MnuItms [2] .DefaultItem = true

NotifyiconMnu = new ContextMenu (mnuItms)

TrayIcon.ContextMenu = notifyiconMnu

/ / add the set ContextMenu object to the tray program

This.SuspendLayout ()

This.AutoScaleBaseSize = new System.Drawing.Size (5,13)

This.ClientSize = new System.Drawing.Size (320,56)

This.ControlBox = false

This.MaximizeBox = false

This.MinimizeBox = false

This.WindowState = System.Windows.Forms.FormWindowState.Minimized

This.Name = "capture"

This.ShowInTaskbar = false

This.Text = "do Screen Capture programs with C #!"

This.ResumeLayout (false)

}

Static void Main ()

{

Application.Run (new Capture ())

}

}

Thank you for your reading, the above is the content of "how to use C # to do Screen Capture program". After the study of this article, I believe you have a deeper understanding of how to use C # to do Screen Capture program, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report