ProcessPictures.cs 4.1 KB
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());
            }
        }
    }
}