WPF如何实现对自定义控件或区域的截图

第一种方法,简单粗暴,但是有个问题,有些控件如果用了winform里的控件来承载,截图显示不了其中的内容,比如视频图像。

                System.Windows.Media.Imaging.RenderTargetBitmap targetBitmap = new System.Windows.Media.Imaging.RenderTargetBitmap((int)this.MulPlayerController.ActualWidth, (int)this.MulPlayerController.ActualHeight, 96d, 96d, System.Windows.Media.PixelFormats.Default);
                targetBitmap.Render(this.gridChilren);
                System.Windows.Media.Imaging.PngBitmapEncoder saveEncoder = new System.Windows.Media.Imaging.PngBitmapEncoder();
                saveEncoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(targetBitmap));
                string tempFile = System.IO.Path.GetTempFileName() + ".png";
                System.IO.FileStream fs = System.IO.File.Open(tempFile, System.IO.FileMode.OpenOrCreate);
                saveEncoder.Save(fs);
                fs.Close();

第二种方法,思路是先将整个屏幕截取下来,如何获得你想要截图区域的坐标和宽高,然后将屏幕中对应的位置区域保存为图片就可以了。

                //获得当前屏幕的分辨率            
                Screen scr = Screen.PrimaryScreen;
                System.Drawing.Rectangle rc = scr.Bounds;
                int iWidth = rc.Width;
                int iHeight = rc.Height;
                //创建一个和屏幕一样大的Bitmap            
                System.Drawing.Image myImage = new System.Drawing.Bitmap((int)this.MulPlayerController.pnlMain.ActualWidth, (int)this.MulPlayerController.pnlMain.ActualHeight);
                //从一个继承自Image类的对象中创建Graphics对象            
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(myImage);
                Point controlPoint = new Point(0, 0);
                controlPoint = this.MulPlayerController.pnlMain.PointToScreen(controlPoint);//获取控件相对于屏幕的位置
                //抓屏并拷贝到myimage里            
                g.CopyFromScreen(new System.Drawing.Point((int)controlPoint.X, (int)controlPoint.Y), new System.Drawing.Point(0, 0), 
                    new System.Drawing.Size((int)this.MulPlayerController.pnlMain.ActualWidth, (int)this.MulPlayerController.pnlMain.ActualHeight));
                //保存为文件         
                myImage.Save("1.jpg");
                string tempFileImg = "1.jpg";
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值