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

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

 更新時(shí)間:2024年01月30日 15:30:32   作者:lingxiao16888  
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xià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)文章

最新評(píng)論

密云县| 上高县| 文登市| 绍兴市| 屏东县| 益阳市| 连南| 永州市| 铜陵市| 平利县| 南靖县| 沁阳市| 柯坪县| 长泰县| 滦平县| 迁西县| 宕昌县| 兰州市| 和静县| 汝阳县| 本溪市| 吉木萨尔县| 汝州市| 唐山市| 双鸭山市| 达拉特旗| 南汇区| 虹口区| 拉孜县| 射洪县| 永安市| 紫金县| 峨眉山市| 临安市| 江华| 河北省| 吉木萨尔县| 乾安县| 德昌县| 卓资县| 蚌埠市|