项目中有一项需求,需要将项目中的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. JavaScript学习(3):函数式编程

    在这篇文章里,我们讨论函数式编程. 什么是函数式编程?根据百度百科的描述,“函数式编程是种编程典范,它将电脑运算视为函数的计算.函数编程语言最重要的基础是 λ 演算(lambda calculus). ...

  2. reset

    html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, ...

  3. IClient for js开发之地图的加载

    进行web开发之前首先需要安装IServer以及iClient for JavaScript的开发包.在这两中都具备的前提下进行第一步,如何调用IServer中发布的服务 调用iServer 中发布的 ...

  4. 设计人员应该看的15个很酷的 iOS 8 设计

    苹果新一代智能手机 iPhone 6 发布已经有一段时间了,一些创意设计师已经开始在设计中采用 iOS 8 设计理念.当然,其中有些是对于未来的展望和大胆的设计.我在这里收集了15个很酷的 iOS 8 ...

  5. 全栈开发必备的10款 Sublime Text 插件

    Sublime Text 具有漂亮的用户界面和强大的功能,例如代码缩略图,多重选择,快捷命令等.Sublime Text 更妙的是它的可扩展性.所以,这里挑选了全栈开发必备的10款 Sublime T ...

  6. 给你推荐10款优秀的 HTML5 动画工具

    HTML5 在过去三年快速增长,已经成为 Web 开发人员最喜欢的编程语言之一.强大的编程语言拥有开发更好的网页应用的能力. HTML5 中引入的新技术都非常好,像 Chrome.Firefox.Sa ...

  7. 请使用java来构造和遍历二叉树?

    [分析] 二叉树的结构:根节点.左子树.右子树.其中左子树的值必须小于根节点,右子树的值必须大于根节点.构造这种树结构,就是创建一个类,并提供一个方法,当给定一个值时,它能够自动创建节点并自动挂到二叉 ...

  8. 如何用JavaScript探测CSS动画是否已经完成

    不啰嗦上代码: WN:(function(){ var el = $('<fakeelement>'), transition="transition", transi ...

  9. 闲聊桌面应用开发[Win16->Win32->ATL/WTL/MFC->WinForm->WPF/Silverlight/WinRT]

    闲来无聊,正好小组人员讨论到桌面的开发,那把笔者接触的WIndows平台下的几个主要的发展过程聊一聊. 主要从概述,参考资料,图书等几个方面说起. 所有的界面开发都会涉及如下的几个方面的内容: v 控 ...

  10. sharepoint:找不到位于 http://XX.XX.XX.XX 的 Web

    自己写了个sharepoint的webservice,发不到IIS上后报错: System.IO.FileNotFoundException: 找不到位于 http://XX.XX.XX.XX 的 W ...