• C# winform实现截屏,附代码


    namespace Message
    {
        partial class Form3
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
                this.SuspendLayout();
                // 
                // Form3
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(353, 347);
                this.Cursor = System.Windows.Forms.Cursors.Cross;
                this.Name = "Form3";
                this.Text = "Form3";
                this.Load += new System.EventHandler(this.Form3_Load);
                this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form3_MouseUp);
                this.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.Form3_DoubleClick);
                this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form3_MouseDown);
                this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form3_MouseMove);
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private System.Windows.Forms.SaveFileDialog saveFileDialog1;
        }
    }

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Message
    {
        public partial class Form3 : Form
        {
            private bool down = false;
            private Point firstPoint;
            private Pen p = new Pen(Color.Red);
            private Graphics gra;
            private Rectangle rectangle;//存储用户截取的矩形
            private FormChat f4;
            public Form3(FormChat f4)
            {
                InitializeComponent();
                this.f4 = f4;
            }
    
            private void Form3_Load(object sender, EventArgs e)
            {
                Image img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);//创建一个和屏幕同样大小的图像
                Graphics g = Graphics.FromImage(img);//绘制这个图像
                //将屏幕绘制到此图像,第一,二个Point是屏幕要截取的左上角的坐标和绘制到图像的左上角的坐标
                g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
                this.BackgroundImage = img;
                this.FormBorderStyle = FormBorderStyle.None;
                this.Bounds = Screen.PrimaryScreen.Bounds;
                gra = this.CreateGraphics();//主要为了用户截取方便
            }
            private void Form3_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    down = true;
                    firstPoint = e.Location;
                }
            }
    
            private void Form3_MouseUp(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    down = false;
                }
                if (e.Button == MouseButtons.Middle)
                {
                    this.Close();
                }
            }
    
            private void Form3_MouseMove(object sender, MouseEventArgs e)
            {
                if (down)
                {
                    gra.DrawImage(this.BackgroundImage, 0, 0);
                    rectangle = new Rectangle(Math.Min(firstPoint.X, e.X), Math.Min(e.Y, firstPoint.Y), Math.Abs(e.X - firstPoint.X), Math.Abs(e.Y - firstPoint.Y));
                    gra.DrawRectangle(p, rectangle);
                }
            }
            private void Form3_DoubleClick(object sender, MouseEventArgs e)
            {
                if (rectangle.Width != 0 && rectangle.Height != 0)
                {
                    gra.DrawImage(this.BackgroundImage, 0, 0);
                    Image im = new Bitmap(rectangle.Width, rectangle.Height);
                    Graphics g = Graphics.FromImage(im);
                    g.CopyFromScreen(rectangle.Location, new Point(0, 0), rectangle.Size);
                    if (((MouseEventArgs)e).Button == MouseButtons.Left)
                    {
                        Clipboard.Clear();
                        Clipboard.SetImage(im);
                        this.f4.Copy();
                        this.Close();
                    }
                }
            }
        }
    }
    

  • 相关阅读:
    搜索进阶1、八数码(HDU1043)
    D.迷宫2 (BFS+优先队列)
    小H的询问(线段树)
    B.迷宫(BFS)
    【UVA】10935 Throwing cards away I(STL队列)
    【UVA】10391 Compound Words(STL map)
    【UVA】12100 Printer Queue(STL队列&优先队列)
    【UVA】1596 Bug Hunt(模拟)
    【UVA】201 Squares(模拟)
    【UVA】1595 Symmetry(模拟)
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234317.html
Copyright © 2020-2023  润新知