Json树状图
public partial class Form_JsonBrower : Form
{
private JObject json;
private JArray jarr;
private string node_value;
public Form_JsonBrower(string? json_str)
{
InitializeComponent();
TreeNode root_node = new(json_str);
AnalysisNodesTree(json_str, root_node);
treeView1.Nodes.Add(root_node);
treeView1.ExpandAll();
}
private void AnalysisNodesTree(string? json_str, TreeNode? parent_node = null)
{
if (json_str == null)
{
return;
}
try
{
json = JsonConvert.DeserializeObject<JObject>(json_str);
if (json != null)
{
foreach (JProperty item in json.Properties())
{
TreeNode node = new TreeNode(item.Name.ToString());
AnalysisNodesTree(item.Value.ToString(), node);
parent_node.Nodes.Add(node);
}
}
}
catch
{
try
{
jarr = JsonConvert.DeserializeObject<JArray>(json_str);
TreeNode node_arr = new TreeNode($"Array [{jarr.Count()}]");
foreach (JObject obj in jarr)
{
TreeNode node = new TreeNode(jarr.IndexOf(obj).ToString());
foreach (JProperty item in obj.Properties())
{
TreeNode sub_node = new TreeNode(item.Name.ToString());
AnalysisNodesTree(item.Value.ToString(), sub_node);
node.Nodes.Add(sub_node);
}
node_arr.Nodes.Add(node);
}
parent_node.Nodes.Add(node_arr);
}
catch
{
parent_node.Nodes.Add(new TreeNode(json_str));
}
}
}
private void treeview_mouse_down(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)//判断你点的是不是右键
{
Point ClickPoint = new Point(e.X, e.Y);
TreeNode CurrentNode = treeView1.GetNodeAt(ClickPoint);
if (CurrentNode != null)//判断你点的是不是一个节点
{
treeView1.SelectedNode = CurrentNode;//选中这个节点
node_value = treeView1.SelectedNode.Text.ToString();//存储节点的文本
if (Tools.IsBase64(node_value))
{
ShowPicToolStripMenuItem.Enabled = true;
}
else
{
ShowPicToolStripMenuItem.Enabled = false;
}
CurrentNode.ContextMenuStrip = contextMenuStrip1;
}
}
}
private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
{
Clipboard.SetDataObject(node_value);
}
private void ShowPicToolStripMenuItem_Click(object sender, EventArgs e)
{
Form_PictureBrower form = new(node_value);
form.ShowDialog();
}
}
partial class Form_JsonBrower
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.treeView1 = new System.Windows.Forms.TreeView();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.CopyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ShowPicToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeView1.Location = new System.Drawing.Point(0, 0);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(800, 450);
this.treeView1.TabIndex = 0;
this.treeView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeview_mouse_down);
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.CopyToolStripMenuItem,
this.ShowPicToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(125, 48);
//
// CopyToolStripMenuItem
//
this.CopyToolStripMenuItem.Name = "CopyToolStripMenuItem";
this.CopyToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.CopyToolStripMenuItem.Text = "复制";
this.CopyToolStripMenuItem.Click += new System.EventHandler(this.CopyToolStripMenuItem_Click);
//
// ShowPicToolStripMenuItem
//
this.ShowPicToolStripMenuItem.Name = "ShowPicToolStripMenuItem";
this.ShowPicToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.ShowPicToolStripMenuItem.Text = "显示图片";
this.ShowPicToolStripMenuItem.Click += new System.EventHandler(this.ShowPicToolStripMenuItem_Click);
//
// Form_JsonBrower
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.treeView1);
this.Name = "Form_JsonBrower";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form_JsonBrower";
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private TreeView treeView1;
private ContextMenuStrip contextMenuStrip1;
private ToolStripMenuItem CopyToolStripMenuItem;
private ToolStripMenuItem ShowPicToolStripMenuItem;
}
简易图片查看器
public partial class Form_PictureBrower : Form
{
public Form_PictureBrower(string base64)
{
InitializeComponent();
pictureBox1.Image = Tools.Base64ToImage(base64);
}
}
partial class Form_PictureBrower
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(800, 450);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// Form_PictureBrower
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.pictureBox1);
this.Name = "Form_PictureBrower";
this.Text = "Form_PictureBrower";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private PictureBox pictureBox1;
}
工具类
internal class Tools
{
public static Image BytesToImg(byte[] buffer)
{
MemoryStream ms = new(buffer);
ms.Position = 0;
Image img = Image.FromStream(ms);
ms.Close();
return img;
}
public static string CharToString(char[] source)
{
byte[] target = new byte[source.Length];
for (int i = 0; i < source.Length; i++)
{
target[i] = (byte)source[i];
}
return Encoding.Unicode.GetString(target);
}
/// <summary>
/// base64 转 Image
/// </summary>
/// <param name="base64"></param>
public static Bitmap Base64ToImage(string base64)
{
try
{
base64 = base64.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");//将base64头部信息替换
byte[] bytes = Convert.FromBase64String(base64);
MemoryStream memStream = new(bytes);
Image mImage = Image.FromStream(memStream);
Bitmap bp = new(mImage);
return bp;
}
catch (Exception e)
{
Tools.WriteLog($"[FUNC]{new StackFrame(1, false)?.GetMethod()?.DeclaringType?.Name }[FUNC][DETAIL]{e.Message}[DETAIL]", LogLevel.ERROR);
return default;
}
}
/// <summary>
/// Image 转成 base64
/// </summary>
/// <param name="fileFullName"></param>
public static string ImageToBase64(string fileFullName)
{
try
{
if (fileFullName == "")
{
return null;
}
Bitmap bmp = new(fileFullName);
MemoryStream ms = new();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length]; ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length); ms.Close();
return Convert.ToBase64String(arr);
}
catch (Exception ex)
{
Tools.WriteLog($"[FUNC]{new StackFrame(1, false)?.GetMethod()?.DeclaringType?.Name }[FUNC][DETAIL]{ex.Message}[DETAIL]", LogLevel.ERROR);
return null;
}
}
private static char[] base64CodeArray = new char[]
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '='
};
/// <summary>
/// 是否base64字符串
/// </summary>
/// <param name="base64Str">要判断的字符串</param>
/// <returns></returns>
public static bool IsBase64(string base64Str)
{
byte[] bytes = null;
return IsBase64(base64Str, out bytes);
}
/// <summary>
/// 是否base64字符串
/// </summary>
/// <param name="base64Str">要判断的字符串</param>
/// <param name="bytes">字符串转换成的字节数组</param>
/// <returns></returns>
public static bool IsBase64(string base64Str, out byte[] bytes)
{
//string strRegex = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
bytes = null;
if (string.IsNullOrEmpty(base64Str))
return false;
else
{
if (base64Str.Contains(","))
base64Str = base64Str.Split(',')[1];
if (base64Str.Length % 4 != 0)
return false;
if (base64Str.Any(c => !base64CodeArray.Contains(c)))
return false;
}
try
{
bytes = Convert.FromBase64String(base64Str);
return true;
}
catch (FormatException)
{
return false;
}
}
}