将treeview控件内容导出图片
项目中有一项需求,需要将项目中的treeview控件展示的树状结构直接导成一张图片。网上方法很多,但很多都是屏幕截屏,我的解决思路是新建一个用户控件,将主窗体的Treeview的数据传给用户控件(不要直接用treeview做参数,可能会有问题),控件中将TreeView放到一个panel中,根据tree的节点深度和叶子节点个数来调整panel的高度和宽度,然后使用panel内置方法导出即可。具体步骤如下:
1. 新建用户控件 控件主要代码如下
public partial class uc_outputtree : UserControl
{
private TreeView treeoutput;
private Panel panelcontent; public uc_outputtree(TreeNode node)
{
InitializeComponent();
this.treeoutput = new TreeView();
treeoutput.Nodes.Clear();
treeoutput.Nodes.Add((TreeNode)node);
treeoutput.ExpandAll();
treeoutput.Dock = DockStyle.Fill; this.panelcontent = new Panel();
this.panelcontent.Location = new System.Drawing.Point(, );
this.panelcontent.Height = tree.getTotalLeafNum(node) * + ;
this.panelcontent.Width = tree.getDeepthTree(node) * + ;
this.panelcontent.Controls.Add(this.treeoutput);
} /// <summary>
/// 定义委托
/// </summary>
public delegate void UCeventOutPut();
/// <summary>
/// 事件
/// </summary>
public event UCeventOutPut UCOutPutEvent; public void TreeOutPut()
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.FilterIndex = ;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出文件到"; DateTime now = DateTime.Now;
saveFileDialog.FileName = now.Year.ToString().PadLeft() + now.Month.ToString().PadLeft(, '')
+ now.Day.ToString().PadLeft(, '') + "-" + now.Hour.ToString().PadLeft(, '')
+ now.Minute.ToString().PadLeft(, '') + now.Second.ToString().PadLeft(, '') + "_datoutput.jpg"; saveFileDialog.Filter = "Jpg Files|*.jpg";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
Rectangle rect = new Rectangle(, , this.treeoutput.ClientRectangle.Width, this.treeoutput.ClientRectangle.Height);
using (Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format16bppRgb555))
{
this.panelcontent.DrawToBitmap(bmp, rect);
bmp.Save(saveFileDialog.FileName, ImageFormat.Jpeg); UCOutPutEvent();
}
}
}
}
2. 主窗体引用控件
internal void DataOutPutPic()
{
uc_outputtree uc_outputtree = new uc_outputtree((TreeNode)this.Tree_Network.Nodes[].Clone());
uc_outputtree.UCOutPutEvent += new uc_outputtree.UCeventOutPut(ucExport_UCOutPutEvent);
uc_outputtree.TreeOutPut();
uc_outputtree.Dispose();
} void ucExport_UCOutPutEvent()
{
MessageBox.Show("导出完成");
}
3. tree的算法
/// <summary>
/// 获取节点深度
/// </summary>
/// <param name="treenode"></param>
/// <returns></returns>
public static int getDeepthTree(TreeNode treenode)
{
int deepth = ;
TreeNode rt = treenode; if (rt.Nodes.Count > )
{
foreach (TreeNode tr in rt.Nodes)
{
int temp = + getDeepthTree(tr);
if (temp > deepth)
{
deepth = temp;
}
}
}
else
{
deepth = ;
} return deepth;
}
/// <summary>
/// 获取所有叶子节点个数
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static int getTotalLeafNum(TreeNode t)
{
int num = ;
if (t.Nodes.Count > )
{
foreach (TreeNode tr in t.Nodes)
{
num += getTotalLeafNum(tr);
}
}
else
{
num = ;
}
return num;
}
将treeview控件内容导出图片的更多相关文章
- C# 把控件内容导出图片
Bitmap newbitmap = new Bitmap(panelW.Width, panelW.Height); panelW.DrawToBitmap(newbitmap ...
- Win32中TreeView控件的使用方法,类似于资源管理器中文件树形显示方式
首先是头文件,内容如下: #include <tchar.h> #include "..\CommonFiles\CmnHdr.h" #include <Wind ...
- WPF学习06:转换控件内容为可存储图片
在图形软件中,我们经常使用到"另存为图片"的功能,本文即介绍如何将WPF控件显示内容转换为图片. , , PixelFormats.Pbgra32); bitmapRender.R ...
- 在TreeView控件节点中显示图片
实现效果: 知识运用: TreeView控件中Nodes集合的Add方法 //创建节点并将节点放入集合中 public virtual TreeNode Add (string key,string ...
- TreeView控件概述、属性与方法
1.作用:用于显示Node结点的分层列表.2.添加到控件箱菜单命令:工程 | 部件,在部件对话框中选择:Microsoft Windows Common Controls 6.03.TreeView控 ...
- TreeView控件使用
treeView1.SelectedNode = treeView1.Nodes[0]; //选中当前treeview控件的根节点为当前节点添加子节点: TreeNode tmp; tmp = n ...
- 基于Treeview控件遍历本地磁盘
一.前言 Treeview控件常用于遍历本地文件信息,通常与Datagridview与ImageList搭配.ImageList控件用于提供小图片给TreeView控件,DatagridView通常显 ...
- 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。
利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...
- C#Winform中treeView控件使用总结
1.如何展开结点时改变图标(注意:不是选中时) 要在目录中使用图标首先要加入一个控件ImageList(命名为imageList1),然后可以按图片的index或名称引用图片. 然后需要在TreeVi ...
随机推荐
- python数据类型详解及列表字典集合推导式详解
一.运算符 Python语言支持以下类型的运算符: 算术运算符 如: #!/usr/bin/env python # -*- coding:utf-8 -*- a = 5 b = 6 print(a ...
- JSCapture – 基于 HTML5 实现的屏幕捕捉库
JSCapture 是用纯 JavaScript 和 HTML5 实现的屏幕捕捉库.它可以让从您的浏览器中截图和记录在桌面的视频.JSCapture 使用 getUserMedia 来实现屏幕捕获.目 ...
- #8.11.16总结#CSS常用样式总结(二)
border 边框 简写:border:1px solid #000; 等效于:border-width:1px;border-style:solid;border-color:#000; 顺序:b ...
- 使用js制作一般网站首页图片轮播效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- SharePoint 2013必备组件离线包安装:AppFabric无法安装问题解决
由于没有网络,无法使用sharepoint2013的安装必备软件的在线下载向导安装,当要安装 SharePoint 2013 的服务器与 Internet 隔离时,通常需要从脱机位置安装必备组件.即使 ...
- SharePoint 2013 直接给AD 组赋权限后,AD组里的用户还是登陆不了SharePoint,提示没有权限
直接给一个all person的AD组赋了个read权限,然后将一个名为“all beijing”的组加到all person组里,但是all beijing组里的人却不能登录sharepoint,提 ...
- Atitit.阿里云c盘 系统盘爆满解决方案
Atitit.阿里云c盘 系统盘爆满解决方案 Use disk parse tool to scan then C:\widnow/soursce /install.wim 迁移 3g 显示在 ...
- sqlmap笔记
sqlmap -u "注入链接" --其他参数或命令 (-v 1表示回显出注入过程) [判断指定字段是否存在注入点]当链接包含两个参数时,可用-p开关选择要注入的参数,例检测id是 ...
- 微信公众号里打开链接下载APP
嵌入这样的代码 <a href="http://a.app.qq.com/o/simple.jsp?pkgname=com.violationquery" target=&q ...
- C语言的基本输入与输出函数(全解)
C语言的基本输入与输出函数 1.1.1 格式化输入输出函数 Turbo C2.0 标准库提供了两个控制台格式化输入. 输出函数printf() 和scanf(), 这两个函数可以在标准输入输出设备上以 ...