最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用C#實(shí)現(xiàn)對(duì)任意區(qū)域任意大小的截圖

 更新時(shí)間:2024年01月30日 15:30:32   作者:lingxiao16888  
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)簡(jiǎn)單的截圖功能,可以對(duì)任意區(qū)域任意大小的截圖,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1.目的

實(shí)現(xiàn)類似系統(tǒng)截圖工具那樣對(duì)屏幕任何區(qū)域自定義大小的截圖。

2.效果展示

點(diǎn)擊截圖

選擇需要截圖的區(qū)域:

區(qū)域選擇完成后,單擊右鍵完成截圖:

在合適的載體上粘貼截圖:

3.代碼

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            //獲取屏幕截屏
            Bitmap bcgImg = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            using (Graphics g= Graphics.FromImage(bcgImg))
            {
                g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(bcgImg.Width, bcgImg.Height));
 
            }
            //將圖片傳給截圖窗口
            CaptureFrm frm = new CaptureFrm(bcgImg);
            frm.TopMost = true;
           if( frm.ShowDialog()== DialogResult.OK)
            {
                MessageBox.Show("截圖已保存至剪貼板,請(qǐng)選擇合適的載體進(jìn)行粘貼!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("取消截圖","提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            this.WindowState = FormWindowState.Normal;
        }
    }
public partial class CaptureFrm : Form
    {
        Bitmap bcgImg;
        bool drawingFlag=false;
        Point startPoint;
        Point endPoint;
        Graphics main_g;
        bool isCapture=false;
        public CaptureFrm(Bitmap img)
        {
            InitializeComponent();
            bcgImg = img;
        }
 
        private void CaptureFrm_Load(object sender, EventArgs e)
        {
            //背景
            this.BackgroundImage = bcgImg;
            this.BackgroundImageLayout = ImageLayout.Stretch;
 
        }
 
        private void CaptureFrm_KeyDown(object sender, KeyEventArgs e)
        {
            //如果按下ESC鍵則退出
            if (e.KeyCode == Keys.Escape)
            {
                //  this.Close();
                DialogResult = DialogResult.Cancel;
            }
        }
 
        private void CaptureFrm_MouseDown(object sender, MouseEventArgs e)
        {
          if(e.Button== MouseButtons.Left)
            {
                drawingFlag = true;
                startPoint = e.Location;
                main_g = this.CreateGraphics();
            }
          if(e.Button== MouseButtons.Right)
            {
                if (isCapture)
                {
                    Bitmap map = new Bitmap(width, height);
                    Bitmap source = new Bitmap(bcgImg, new Size(this.Width, this.Height));
                    using (Graphics g = Graphics.FromImage(map))
                    {
                        g.DrawImage(source, new Rectangle(0, 0, map.Width, map.Height), new Rectangle(recX, recY, width, height), GraphicsUnit.Pixel);
                    }
                    Clipboard.SetImage(map);
                    main_g.Dispose();
                    DialogResult = DialogResult.OK;
                    isCapture = false;
                }
            }
            
            
        }
 
        private void CaptureFrm_MouseUp(object sender, MouseEventArgs e)
        {
            if(e.Button== MouseButtons.Left)
            {
                //進(jìn)行最終邊界確認(rèn)
                endPoint = e.Location;
                drawingFlag = false;
                isCapture = true;
            }
           
        }
        int recX, recY, width, height;
        private void CaptureFrm_MouseMove(object sender, MouseEventArgs e)
        {
            if (!drawingFlag ||main_g==null) return;
            
            width = Math.Abs(startPoint.X - e.X);
            height = Math.Abs(startPoint.Y - e.Y);
            if (startPoint.X < e.X)
            {
                recX = startPoint.X;
            }
            else
            {
                recX = startPoint.X-width;
            }
            if (startPoint.Y < e.Y)
            {
                recY = startPoint.Y;
            }
            else
            {
                recY = startPoint.Y-height;
            }
            CaptureFrm_Paint(null, null);
            Pen pen = new Pen(Color.Green, 2);
            pen.DashPattern = new float[] { 1, 2 };
            pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
            main_g.DrawRectangle(pen, recX, recY, width, height);
            string ss = $"X:{recX},Y:{recY}\n width:{width},height:{height}";
            main_g.DrawString(ss, new Font("宋體", 12,FontStyle.Bold), Brushes.Red, new Point(10, 10));
        }
 
        private void CaptureFrm_Paint(object sender, PaintEventArgs e)
        {
            if (main_g == null) return;
          //  main_g.Clear(Color.WhiteSmoke);
            main_g.DrawImage(bcgImg, new Rectangle(0, 0, this.Width, this.Height), new Rectangle(0, 0, bcgImg.Width, bcgImg.Height), GraphicsUnit.Pixel);
           
        }
 
        private void CaptureFrm_DoubleClick(object sender, EventArgs e)
        {
           
        }
    }

到此這篇關(guān)于使用C#實(shí)現(xiàn)對(duì)任意區(qū)域任意大小的截圖的文章就介紹到這了,更多相關(guān)C#截圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#如何實(shí)現(xiàn)圖片的剪裁并保存

    C#如何實(shí)現(xiàn)圖片的剪裁并保存

    基于c#實(shí)現(xiàn)圖片的裁剪并保存功能,實(shí)現(xiàn)方法非常簡(jiǎn)單的,前端采用的cropper插件,但是在本文中沒(méi)有給大家多介紹,需要的朋友可以到腳本之家去查找這個(gè)插件。好了,如果大家對(duì)c#實(shí)現(xiàn)圖片裁剪并保存功能感興趣的朋友一起看看吧
    2016-11-11
  • C# BinaryReader實(shí)現(xiàn)讀取二進(jìn)制文件

    C# BinaryReader實(shí)現(xiàn)讀取二進(jìn)制文件

    在 C# 以二進(jìn)制形式讀取數(shù)據(jù)時(shí)使用的是 BinaryReader 類。本文介紹了C# BinaryReader實(shí)現(xiàn)讀取二進(jìn)制文件,感興趣的可以了解一下
    2021-06-06
  • C#通過(guò)流寫入數(shù)據(jù)到文件的方法

    C#通過(guò)流寫入數(shù)據(jù)到文件的方法

    這篇文章主要介紹了C#通過(guò)流寫入數(shù)據(jù)到文件的方法,涉及C#通過(guò)字節(jié)流讀寫文件的相關(guān)技巧,需要的朋友可以參考下
    2015-07-07
  • 如何使用C#代碼創(chuàng)建快捷方式文件詳解

    如何使用C#代碼創(chuàng)建快捷方式文件詳解

    在Windows中創(chuàng)建快捷方式很簡(jiǎn)單,如果想用C#代碼的方式創(chuàng)建,就沒(méi)有那么方便了,因?yàn)?NET框架沒(méi)有提供直接創(chuàng)建快捷方式的方法。這篇文章主要給大家介紹了關(guān)于如何使用C#代碼創(chuàng)建快捷方式文件的相關(guān)資料,需要的朋友可以參考下
    2018-08-08
  • 最新評(píng)論

    五峰| 黔西| 鲁山县| 定陶县| 澎湖县| 公安县| 旬邑县| 陕西省| 澄江县| 阜宁县| 凭祥市| 陇川县| 凭祥市| 洪洞县| 原阳县| 嘉义市| 三河市| 肥东县| 雷州市| 山丹县| 铅山县| 浠水县| 横山县| 清水河县| 儋州市| 丹棱县| 灵川县| 乌拉特中旗| 延边| 常熟市| 哈密市| 通州区| 三都| 朝阳区| 阜康市| 远安县| 长岭县| 兴义市| 阿尔山市| 大田县| 大理市|