ProcessPictures.cs
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace TSA_V.Common
{
public class ProcessPictures
{
/// <summary>
/// 获取图片二进制流
/// </summary>
/// <param name="root"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public static MemoryStream GetImage(string root,string fileName)
{
System.IO.MemoryStream sm = new MemoryStream();
try
{
string imagePath = ConfigAppSettings.GetValue(Setting_Init.BOARD_IMAGE_PATH);
XmlDocument doc = new XmlDocument();
string file = root + imagePath + @"\" + fileName + "Image.xml";
if (File.Exists(file))
{
doc.Load(file);
XmlNodeList NodeList = doc.GetElementsByTagName("BoardImage");//得到节点列表
XmlNode ImageNode = NodeList[0];//得到该节点
string PicByte = ImageNode.InnerXml;//得到节点内的二进制代码
byte[] b = Convert.FromBase64String(PicByte);//转化为byte[]
sm.Write(b, 0, b.Length);//写到流中
}
else
{
return null;
}
}
catch { }
return sm;
}
/// <summary>
/// 保存图片二进制流
/// </summary>
/// <param name="root"></param>
/// <param name="BoardName"></param>
/// <param name="br"></param>
/// <returns></returns>
public static bool SaveImage(string root, string BoardName, BinaryReader br)
{
try
{
int readByte = 0;
int bytesToRead = 100;
string imagePath = ConfigAppSettings.GetValue(Setting_Init.BOARD_IMAGE_PATH);
if (!Directory.Exists(root + BoardName))//如果不存在就创建file文件夹
{
Directory.CreateDirectory(root + BoardName);
}
XmlTextWriter xmlTxtWt = new XmlTextWriter(root + imagePath + @"\" + BoardName + "Image.xml", Encoding.UTF8);
//输出设置代码缩进
xmlTxtWt.Formatting = Formatting.Indented;
xmlTxtWt.WriteStartDocument();
xmlTxtWt.WriteStartElement(BoardName);//定义命名空间
xmlTxtWt.WriteStartElement("BoardImage"); //定义节点
byte[] base64buffer = new byte[bytesToRead]; //开辟缓冲区
do
{
readByte = br.Read(base64buffer, 0, bytesToRead); //将数据读入字节数组
xmlTxtWt.WriteBase64(base64buffer, 0, readByte); //将数组中二进制值编码为Base64并写出到XML文件
} while (bytesToRead <= readByte);
xmlTxtWt.WriteEndElement();
xmlTxtWt.WriteEndElement();
xmlTxtWt.WriteEndDocument();
xmlTxtWt.Close();
return true;
}
catch
{
return false;
}
}
public static void copyImage(string root , string boardName,string originPicPath)
{
string imagePath = ConfigAppSettings.GetValue(Setting_Init.BOARD_IMAGE_PATH);
string fallPath = root + "/" + imagePath + @"\" + boardName + Path.GetExtension(originPicPath);
try
{
string path = root + "/" + imagePath;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
File.Copy(originPicPath, fallPath,true);
}
catch (Exception ex)
{
LogUtil.error("复制 图片【"+originPicPath+"】到【"+ fallPath + "】出错:" + ex.ToString());
}
}
}
}