C#螢幕截圖的實現

來源:互聯網
上載者:User

先給你的程式添加一個Windows表單 ,Name:ScreenBody   TopMost:true    WindowState:Maximized

下面是一些欄位定義,事件函數和輔助函數:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LCY.XY
{
    public partial class ScreenBody : Form
    {
        /// <summary>
        /// 主畫筆
        /// </summary>
        private Graphics mainPainter;
        /// <summary>
        /// 筆
        /// </summary>
        private Pen pen;
        /// <summary>
        /// 判斷滑鼠是否按下
        /// </summary>
        private bool isDowned;
        /// <summary>
        /// 矩形是否繪製完成
        /// </summary>
        private bool rectReady;
        /// <summary>
        /// 原始畫面
        /// </summary>
        private Image baseImage;
        /// <summary>
        /// 要儲存的畫面
        /// </summary>
        private Rectangle rect;
        /// <summary>
        /// 滑鼠按下的點
        /// </summary>
        private Point downPoint;
        private int tmpx;
        private int tmpy;

        public ScreenBody()
        {
            InitializeComponent();
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScreenBody_DoubleClick(object sender, EventArgs e)
        {
            if (((MouseEventArgs)e).Button == MouseButtons.Left && rect.Contains(((MouseEventArgs)e).X, ((MouseEventArgs)e).Y))
            {
                Image memory = new Bitmap(rect.Width, rect.Height);
                Graphics g = Graphics.FromImage(memory);
                g.CopyFromScreen(rect.X + 1, rect.Y + 1, 0, 0, rect.Size);
                Clipboard.SetImage(memory);
                this.Close();
            }
        }

        /// <summary>
        /// 按下滑鼠
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScreenBody_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDowned = true;
                if (!rectReady)
                {
                    rect.X = e.X;
                    rect.Y = e.Y;
                    downPoint = new Point(e.X, e.Y);
                }
                if (rectReady)
                {
                    tmpx = e.X;
                    tmpy = e.Y;
                }
            }
            if (e.Button == MouseButtons.Right)
            {
                if (!rectReady)
                {
                    this.Close();
                    return;
                }
                mainPainter.DrawImage(baseImage, 0, 0);
                rectReady = false;
            }
        }

        /// <summary>
        /// 鬆開滑鼠
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScreenBody_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (e.X < rect.X)
                {
                    rect.X = e.X;
                }
                if (e.Y < rect.Y)
                {
                    rect.Y = e.Y;
                }
                isDowned = false;
                rectReady = true;
            }
        }

        /// <summary>
        /// 移動滑鼠
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScreenBody_MouseMove(object sender, MouseEventArgs e)
        {
            if (!rectReady && isDowned)
            {
                Image newImage = drawScreen((Image)baseImage.Clone(), e.X, e.Y);
                mainPainter.DrawImage(newImage, 0, 0);
                newImage.Dispose();
            }
            if (rectReady)
            {
                if (rect.Contains(e.X, e.Y))
                {
                    if (isDowned)
                    {
                        rect.X = rect.X + e.X - tmpx;
                        rect.Y = rect.Y + e.Y - tmpy;
                        tmpx = e.X;
                        tmpy = e.Y;
                        moveRect((Image)baseImage.Clone(), rect);
                    }
                }
            }
        }

        /// <summary>
        /// 畫矩形
        /// </summary>
        /// <param name="Painter"></param>
        /// <param name="mouse_x"></param>
        /// <param name="mouse_y"></param>
        private void drawRect(Graphics Painter, int mouse_x, int mouse_y)
        {
            int width = 0;
            int heigth = 0;
            if (mouse_y < rect.Y)
            {
                heigth = downPoint.Y - mouse_y;
            }
            else
            {
                heigth = mouse_y - downPoint.Y;
            }

            if (mouse_x < rect.X)
            {
                width = downPoint.X - mouse_x;
            }
            else
            {
                width = mouse_x - downPoint.X;
            }
            rect.Size = new Size(width, heigth);
            Painter.DrawRectangle(pen, minOf(mouse_x, downPoint.X), minOf(mouse_y, downPoint.Y), width, heigth);
        }

        /// <summary>
        /// 選地區時畫矩形
        /// </summary>
        /// <param name="back"></param>
        /// <param name="mouse_x"></param>
        /// <param name="mouse_y"></param>
        /// <returns></returns>
        private Image drawScreen(Image back, int mouse_x, int mouse_y)
        {
            Graphics painter = Graphics.FromImage(back);
            drawRect(painter, mouse_x, mouse_y);
            return back;
        }
       
        /// <summary>
        /// 選好地區後移動矩形
        /// </summary>
        /// <param name="image"></param>
        /// <param name="rect2"></param>
        private void moveRect(Image image, Rectangle rect2)
        {
            Graphics painter = Graphics.FromImage(image);
            painter.DrawRectangle(pen, rect2.X, rect2.Y, rect2.Width, rect2.Height);
            mainPainter.DrawImage(image, 0, 0);
            image.Dispose();
        }

        /// <summary>
        /// 初始設定變數
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScreenBody_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            mainPainter = this.CreateGraphics();
            pen = new Pen(Brushes.Blue);
            isDowned = false;
            baseImage = this.BackgroundImage;
            rect = new Rectangle();
            rectReady = false;
        }

        /// <summary>
        /// 返回較小的數
        /// </summary>
        /// <param name="f1"></param>
        /// <param name="f2"></param>
        /// <returns></returns>
        private float minOf(float f1, float f2)
        {
            if (f1 > f2)
                return f2;
            else
                return f1;
        }
    }
}

最後,在需要的地方調用以下函數:

        /// <summary>
        /// 顯示視窗
        /// </summary>
        void GetBitmap()
        {
            Image img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
            Graphics g = Graphics.FromImage(img);
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
            ScreenBody body = new ScreenBody();
            body.BackgroundImage = img;
            body.Show();
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.