C# WinForm控件及其子控件转成图片(支持带滚动条的长截图)

概述(Overview)

参考了网上的分享,感觉都不太理想:1.一个控件内如果包含多个子控件时没有考虑顺序问题;2.超出控件可显示区域时不能长截图,有滚动条会多余截取了滚动条。这个随笔旨在解决这个问题,实现带滚动条时可以长截图,并且给出了在多个子控件的情况下如何控制截图顺序的代码。有用可以点个赞。引用本文章请注明出处,谢谢。

(Referring to the Share on the Internet, I feel that it is not ideal: 1. If there are multiple sub-controls in a control, the order is not considered; 2. When the display area of the control is exceeded, the screenshot cannot be long, and the scroll bar will be redundantly intercepted. This essay aims to solve this problem by implementing long screenshots with scrollbars, and gives code on how to control the order of screenshots in the case of multiple child controls. If it's useful, you can like it. Please indicate the source for citing this article, thank you.)

复制代码
 1         /// <summary>
 2         /// 绘制整个控件以及子控件
 3         /// </summary>
 4         /// <param name="ctrl">窗口/控件</param>
 5         /// <param name="bitmap">bit图片</param>
 6         /// <returns></returns>
 7         private Bitmap DrawToBitmap(Control ctrl, Bitmap bitmap = null)
 8         {
 9             //获取整个界面的大小
10             var w = ctrl.DisplayRectangle.Width;
11             var h = ctrl.DisplayRectangle.Height;
12 
13             //长截图
14             if (bitmap == null)
15             {
16                 bitmap = ctrl.BackgroundImage == null ? new Bitmap(w, h) : new Bitmap(ctrl.BackgroundImage);
17             }
18 
19             if (ctrl.HasChildren)
20             {
21                 //绘制子控件内容(逆序处理:图层从下开始往上叠取)
22                 for (int i = ctrl.Controls.Count - 1; i >= 0; i--)
23                 {
24                     var control = ctrl.Controls[i];
25                     //重新计算图片在bitmap上的位置
26                     var newLocation = new Point(control.Location.X - ctrl.DisplayRectangle.X, control.Location.Y - ctrl.DisplayRectangle.Y);
27 
28                     using (Bitmap temp = new Bitmap(control.Width, control.Height))
29                     using (Graphics g = Graphics.FromImage(bitmap))
30                     {
31                         control.DrawToBitmap(temp, new Rectangle(new Point(0, 0), control.Size));
32                         g.DrawImage(temp, new Rectangle(newLocation, control.Size));
33                     }
34                     DrawToBitmap(control, bitmap);
35                 }
36             }
37 
38             return bitmap;
39         }

Code

引用(Reference)

相关推荐
江沉晚呤时5 小时前
在 C# 中调用 Python 脚本:实现跨语言功能集成
python·microsoft·c#·.net·.netcore·.net core
Oberon5 小时前
Avalonia硬配.NET Framework 4.8
c#·.net·avalonia·.net framework
喵叔哟8 小时前
3. 【Blazor全栈开发实战指南】--Blazor是什么?为什么选择Blazor?
c#·.netcore
钢铁男儿10 小时前
C# 接口(接口可以继承接口)
java·算法·c#
小码编匠18 小时前
C# 的西门子数控系统 OPCUA 数据采集开发从零入门
后端·数据分析·c#
孜然卷k18 小时前
C#项目 在Vue/React前端项目中 使用使用wkeWebBrowser引用并且内部使用iframe网页外链 页面部分白屏
前端·vue.js·react.js·c#
专注VB编程开发20年19 小时前
C# VB.NET多进程-管道通信,命名管道(Named Pipes)
开发语言·c#·.net
唐青枫20 小时前
C#.NET 泛型详解
c#·.net
阿蒙Amon20 小时前
C#日期、时间和时区:全球化应用的时间处理艺术
java·服务器·c#
学不动CV了20 小时前
深入理解C语言内存空间、函数指针(三)(重点是函数指针)
c语言·arm开发·数据库·stm32·单片机·嵌入式硬件·c#