马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#实现屏幕截图,跟着鼠标显示图片

[复制链接]
查看4700 | 回复1 | 2014-12-19 09:34:20 | 显示全部楼层 |阅读模式
需要两个picturebox,一个放图片  【转载博客园 ,代码未测试】

完整代码如下



[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Xml;
namespace Test
{
    public partial class Form1 : Form
    {
        private bool MouseIsDown = false;
        private int _x, _y;
        private Rectangle MouseRect = Rectangle.Empty;
        public Form1()
        {
            InitializeComponent();
        }
        private void ResizeToRectangle(Point p)
        {
            DrawRectangle();
            MouseRect.Width = p.X - MouseRect.Left;
            MouseRect.Height = p.Y - MouseRect.Top;
            DrawRectangle();
        }
        private void DrawRectangle()
        {
            Rectangle rect = this.RectangleToScreen(MouseRect);
            ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed);

        }
        private void DrawStart(Point StartPoint)
        {
            barkPictureBox.Capture = true;
            Cursor.Clip = this.RectangleToScreen(new Rectangle(0, 0, ClientSize.Width, ClientSize.Height));
            MouseRect = new Rectangle(StartPoint.X, StartPoint.Y, 0, 0);
        }


        private void button1_Click(object sender, EventArgs e)
        {

        }


        void barkPictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            barkPictureBox.Capture = false;
            Cursor.Clip = Rectangle.Empty;
            MouseIsDown = false;
            DrawRectangle();

            if (MouseRect.X == 0 || MouseRect.Y == 0 || MouseRect.Width == 0 || MouseRect.Height == 0)  //如果区域有一个是0就返回
            {
            }
            else
            {
                ///代码
            }

            MouseRect = Rectangle.Empty;
            int sx = _x < e.X ? _x : e.X;
            int sy = _y < e.Y ? _y : e.Y;
            int w = Math.Abs(_x - e.X);
            int h = Math.Abs(_y - e.Y);
            Graphics g = Graphics.FromHwnd(pictureBox2.Handle);
            g.Clear(pictureBox2.BackColor);
            g.DrawImage(barkPictureBox.Image, new Rectangle(0, 0, w, h), sx, sy, w, h, GraphicsUnit.Pixel); 
        }

        void barkPictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseIsDown) ResizeToRectangle(new Point(e.X + barkPictureBox.Location.X, e.Y + barkPictureBox.Location.Y));
           
        }

        void barkPictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            _x = e.X;
            _y = e.Y;

            MouseIsDown = true;
            DrawStart(new Point(e.X + barkPictureBox.Location.X, e.Y + barkPictureBox.Location.Y));
        }


 

   

        private void Form1_Load(object sender, EventArgs e)
        {
            barkPictureBox.MouseDown += new MouseEventHandler(barkPictureBox_MouseDown);
            barkPictureBox.MouseMove += new MouseEventHandler(barkPictureBox_MouseMove);
            barkPictureBox.MouseUp += new MouseEventHandler(barkPictureBox_MouseUp);

        }
    }
}

C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
condon | 2014-12-20 10:14:28 | 显示全部楼层
运行没反应啊!
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则