项目中有一项需求,需要将项目中的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. Git基本使用教程

    1.创建版本库      版本库又可以称为仓库(repository),可以简单理解为一个目录,在这个目录下的所有文件都可以被git管理起来,每个文件的新增.修改.删除Git都可以跟踪,以便在任何时刻 ...

  2. WPF实现炫酷Loading控件

    Win8系统的Loading效果还是很不错的,网上也有人用CSS3等技术实现,研究了一下,并打算用WPF自定义一个Loading控件实现类似的效果,并可以让用户对Loading的颗粒(Particle ...

  3. HTML Minifier - 灵活的在线 HTML 压缩工具

    HTML Minifier 是一个高度可配置的,经过良好测试的,基于 JavaScript 的 HTML 在线压缩工具,用棉绒般的能力.在它的核心, Minifier 依赖于 John Resig 的 ...

  4. Spirit - 腾讯移动 Web 整体解决方案

    Spirit 并不是一个具体的框架或者工具,但是她是移动端一系列解决方案的整合与聚拢.她是腾讯 Alloyteam 开发团队在移动开发项目中通过大量实践.归纳.总结提炼而成,最终沉淀下来的一个体系,真 ...

  5. GifShot - 创建动态 GIF 的 JavaScript 库

    GifShot 是一个可以创建流媒体,视频或图像的 GIF 动画的 JavaScript 库.该库的客户端特性使其非常便携,易于集成到几乎任何网站.利用最先进的浏览器 API ,包括 WebRTC , ...

  6. [deviceone开发]-购物车的简单示例

    一.简介 主要是演示listview所在的ui和模板cell所在的ui之间数据的交互,点击一行,可以通过加减数量,自动把所有选中的汽车价格显示在底部. 二.效果图 三.示例地址: http://sou ...

  7. MySQL sharding的几个参考地址

    http://stackoverflow.com/questions/5541421/mysql-sharding-approaches http://www.oschina.net/search?s ...

  8. html前端总结

    1.设置图片的最大宽高的css<style> .img-max { max-height:50px; width:expression(document.body.clientHeight ...

  9. webView 显示一段 html 代码

    1.布局文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:and ...

  10. Android SurfaceView的生命周期

    本文利用SurfaceView来实现视频的播放 本文地址:http://www.cnblogs.com/wuyudong/p/5851156.html,转载请注明源地址. 在main.xml布局文件添 ...