winfrom 截屏、抓屏 分类: WinForm ...

截取全屏代码:

try
            {
                this.Hide();
                Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty));
                Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
                Graphics g = Graphics.FromImage(bitmap);
                g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);

                System.Threading.Thread.Sleep(50);

                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "bmp files (*.bmp)|*.bmp";
                saveFileDialog.Title = "保存文件";
                saveFileDialog.ShowDialog();
                bmpPath = saveFileDialog.FileName;
                if ("" != bmpPath)
                {
                    bitmap.Save(bmpPath, ImageFormat.Bmp);
                }
                bitmap.Dispose();
                this.Show();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("抓图失败!");
                this.Show();
            }

frmChildScreen 窗体代码如下:

 private void frmChildScreen_Load(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Cross;

            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
            this.UpdateStyles();
            originBmp = new Bitmap(this.BackgroundImage);
        }

        private void Catch_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (!catchStart)
                {
                    catchStart = true;
                    startPoint = new Point(e.X, e.Y);
                }
            }
        }

        private void Catch_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void Catch_MouseMove(object sender, MouseEventArgs e)
        {
            if (catchStart)
            {
                Bitmap destBmp = (Bitmap)originBmp.Clone();
                Point newPoint = new Point(startPoint.X, startPoint.Y);
                Graphics g = Graphics.FromImage(destBmp);
                Pen p = new Pen(Color.Blue, 1);
                int width = Math.Abs(e.X - startPoint.X), height = Math.Abs(e.Y - startPoint.Y);
                if (e.X < startPoint.X)
                {
                    newPoint.X = e.X;
                }
                if (e.Y < startPoint.Y)
                {
                    newPoint.Y = e.Y;
                }
                catchRect = new Rectangle(newPoint, new Size(width, height));
                g.DrawRectangle(p, catchRect);
                g.Dispose();
                p.Dispose();
                Graphics g1 = this.CreateGraphics();
                g1 = this.CreateGraphics();
                g1.DrawImage(destBmp, new Point(0, 0));
                g1.Dispose();
                destBmp.Dispose();
            }
        }


        private void Catch_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (catchStart)
                {
                    catchStart = false;
                    catchFinish = true;
                }
            }
        }

        private void Catch_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && catchFinish)
            {
                if (catchRect.Contains(new Point(e.X, e.Y)))
                {
                    Bitmap bitmap = new Bitmap(catchRect.Width, catchRect.Height);
                    Graphics g = Graphics.FromImage(bitmap);
                    g.DrawImage(originBmp, new Rectangle(0, 0, bitmap.Width, bitmap.Height), catchRect, GraphicsUnit.Pixel);

                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    saveFileDialog.Filter = "bmp files (*.bmp)|*.bmp";
                    saveFileDialog.Title = "保存文件";
                    saveFileDialog.ShowDialog();
                    bmpPath = saveFileDialog.FileName;
                    if ("" != bmpPath)
                    {
                        bitmap.Save(bmpPath, ImageFormat.Bmp);
                    }
                    bitmap.Dispose();
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
        }

截取局部屏幕代码如下:

 try
{
this.Hide();
Rectangle bounds
= Screen.GetBounds(Screen.GetBounds(Point.Empty));

Thread.Sleep(
50);
frmChildScreen CatchForm
= new frmChildScreen();
Bitmap catchBmp
= new Bitmap(bounds.Width, 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)
{
this.Show();
}

}
catch (System.Exception e)
{
MessageBox.Show(
"抓图失败!");
this.Show();
}

转载于:https://www.cnblogs.com/Jackerson/p/4632012.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值