项目中有一项需求,需要将项目中的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控件内容导出图片的更多相关文章

  1. C# 把控件内容导出图片

    Bitmap newbitmap = new Bitmap(panelW.Width, panelW.Height);            panelW.DrawToBitmap(newbitmap ...

  2. Win32中TreeView控件的使用方法,类似于资源管理器中文件树形显示方式

    首先是头文件,内容如下: #include <tchar.h> #include "..\CommonFiles\CmnHdr.h" #include <Wind ...

  3. WPF学习06:转换控件内容为可存储图片

    在图形软件中,我们经常使用到"另存为图片"的功能,本文即介绍如何将WPF控件显示内容转换为图片. , , PixelFormats.Pbgra32); bitmapRender.R ...

  4. 在TreeView控件节点中显示图片

    实现效果: 知识运用: TreeView控件中Nodes集合的Add方法 //创建节点并将节点放入集合中 public virtual TreeNode Add (string key,string ...

  5. TreeView控件概述、属性与方法

    1.作用:用于显示Node结点的分层列表.2.添加到控件箱菜单命令:工程 | 部件,在部件对话框中选择:Microsoft Windows Common Controls 6.03.TreeView控 ...

  6. TreeView控件使用

    treeView1.SelectedNode = treeView1.Nodes[0];  //选中当前treeview控件的根节点为当前节点添加子节点:  TreeNode tmp; tmp = n ...

  7. 基于Treeview控件遍历本地磁盘

    一.前言 Treeview控件常用于遍历本地文件信息,通常与Datagridview与ImageList搭配.ImageList控件用于提供小图片给TreeView控件,DatagridView通常显 ...

  8. 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。

    利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...

  9. C#Winform中treeView控件使用总结

    1.如何展开结点时改变图标(注意:不是选中时) 要在目录中使用图标首先要加入一个控件ImageList(命名为imageList1),然后可以按图片的index或名称引用图片. 然后需要在TreeVi ...

随机推荐

  1. Test your application

    Creating automatic test suites for your application is a good way to make it robust. It allows you t ...

  2. js的一些属性

    js attribute(): setAttribute():element.setAttribute(name,balue) getAttribute():element.getAttribute( ...

  3. 20款响应式的 HTML5 网页模板【免费下载】

    下面的列表集合了20款响应式的 HTML5 网页模板,这些专业的模板能够让你的网站吸引很多的访客.除了好看的外观,HTML5 模板吸引大家的另一个原因是由于其响应性和流动性.赶紧来看看. 您可能感兴趣 ...

  4. CSS级联和继承

    2016-11-06 <CSS入门经典>第七章 1.在HTML中使用CSS样式表的三种方式: (1)内联的样式表. eg:<em style="background-whi ...

  5. scroll事件实现监控滚动条并分页显示示例(zepto.js)

    scroll事件实现监控滚动条并分页显示示例(zepto.js  ) 需求:在APP落地页上的底部位置显示此前其他用户的购买记录,要求此div盒子只显示3条半,但一页有10条,div内的滑动条滑到一页 ...

  6. Mysql一些复杂的语句

    1.查找重复的行 SELECT * FROM blog_user_relation a WHERE (a.account_instance_id,a.follow_account_instance_i ...

  7. datagridview的数据存取

    这里主要是复习DataSet等数据或ADO.NET方面的知识.下面是一个简单的数据存储(在DataGridView上增加一行然后并存储到数据库的过程): private void button1_Cl ...

  8. R语言学习

    1.清屏 Ctrl + L 2.退出 q() 3.设置工作空间 getwd() setwd('D:\\Program Files\\RStudio\\workspace') 4.显档当前工作目录下的文 ...

  9. Gartner:用自适应安全架构来应对高级定向攻击

    发表于2015-06-24   摘要:当前的防护功能难以应对高级的定向攻击,由于企业系统所受到的是持续攻击,并持续缺乏防御力,面向“应急响应”的特别方式已不再是正确的思维模式,Garnter提出了用自 ...

  10. 转大写字母-(ASCII表)

    #include<stdio.h> /* 转大写,ASCII表.大写与小写相差32 */ char upper(char c) { if(c>='a'&&c<= ...