C# GDI+绘图(三)GDI+实现QQ截图类似功能

前两篇,介绍了GDI+的一些基本用法,现在我们来试着实现一个模仿qq截图的功能来加深一下对GDI+的理解和运用。

首先,我们介绍一下思路:

 聊天窗体上有一个截图按钮,点击按钮后,程序将整个屏幕画在一个新的全屏窗体上,然后显示这个窗体.因为是全屏的窗体,并且隐藏了菜单栏、工具栏等,所以在我们看来就好像是一个桌面的截图,然后在这个新窗体上画矩形,最后保存矩形中的内容并显示在原来的聊天窗体中.

废话不说,先来上一个我的界面截图

其次,我贴出部分主要代码:

Catch页面:

#region 变量
        private Point DownPoint = Point.Empty;//确定绘图起点,按下的坐标
        private bool CatchFinished = false;//是否完成截图
        private bool CatchStart = false;//是否开始截图
        private Bitmap originBmp;//用来保存原始图像
        private Rectangle CatchRect;//用来保存截图的矩形
        #endregion

        public Catch()
        {
            InitializeComponent();
            this.Load += Catch_Load;
            this.MouseClick += Catch_MouseClick;
            this.MouseDown += Catch_MouseDown;
            this.MouseMove += Catch_MouseMove;
            this.MouseUp += Catch_MouseUp;
            this.MouseDoubleClick += Catch_MouseDoubleClick;
        }

        void Catch_Load(object sender, EventArgs e)
        {
            //设置控件样式为双缓冲,以有效减少图片闪烁
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
            this.UpdateStyles();

            originBmp = new Bitmap(this.BackgroundImage);
        }

Main页面:

private void btn_screenShot_Click(object sender, EventArgs e)
        {
            if (cb_hideCurrent.Checked)
	        {
		        this.Hide();
	        }
            Thread.Sleep(1000);
            Catch catchForm = new Catch();
            Bitmap CatchBmp = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
            Graphics g = Graphics.FromImage(CatchBmp);
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height));
            catchForm.BackgroundImage = CatchBmp;
            if (catchForm.ShowDialog() == DialogResult.OK)
            {
                IDataObject iData = Clipboard.GetDataObject();
                DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Bitmap);
                if (iData.GetDataPresent(DataFormats.Bitmap))
                {
                    richTextBox1.Paste(myFormat);
                    Clipboard.Clear();
                }
                this.Show();
            }
        }

至此,我们就完成了一个模仿QQ截图的功能,完整的项目文件我放在了GUI+绘图实现截屏功能,欢迎大家下载。

或者关注微信公众号IronMarmot,后台回复“截图”即可。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值