c# 程序窗体软件截图(稳如狗,不推荐使用API)

推荐如下代码:  窗体被遮挡也不影响,但是有一个bug  绘画显示在前面的控件程序上界面上要设置到底层   (底层控件优先显示)

--效率高,稳如狗

   TitleForm_Big titleForm= new TitleForm_Big();
   //创建一个全局画布
   Bitmap bmp = new Bitmap(1280, 720);

 private void MainForm_Load(object sender, EventArgs e)
 {
   titleForm.Show();
   titleForm.Location = new Point(0, -from.Size.Width);
  }
  private void TransferTimer_Tick(object sender, EventArgs e)
   {
            TransferTimer.Enabled = false;
            try
            {
                 //释放画布放置内存积累
                 bmp?.Dispose();
                 bmp = new Bitmap(1280, 720);
                 //绘画
                  titleForm.DrawToBitmap(bmp, new Rectangle(new Point(0, 0), titleForm.Size));
                  //显示
                  pictureBox1.Image = bmp;
            }
            catch (Exception ex)
            {
                mt.Show(ex.ToString(), true);
            }
            TransferTimer.Enabled = true;
    }

------------------------------下面为WinAPI  截图  不推荐,内存会骤增----------------------------------

        /// <summary>
        /// 截屏
        /// </summary>
        /// <param name="ctrl"></param>
        /// <returns></returns>
        [DllImport("User32.dll")]
        private static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
        public Bitmap CaptureControl(Control ctrl)
        {
            if (ctrl == null || ctrl.IsDisposed)
            {
                return null;
            }
            Bitmap image = new Bitmap(ctrl.Width, ctrl.Height);
            using (Graphics graphics = Graphics.FromImage(image))
            {
                IntPtr hdc = graphics.GetHdc();
                try
                {
                    if (!PrintWindow(ctrl.Handle, hdc, 0))
                    {
                        return null;
                    }
                }
                catch (Exception ex)
                {
                    mt.Show("异常CaptureControl:" + ex.ToString(), true);
                }
                finally
                {
                    graphics.ReleaseHdc(hdc);
                }
            }
            return image;
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fanwenhu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值