IO文件流与序列化

97 阅读7分钟
  1. 获取文件信息
 // 检测文件夹是否存在
 Directory.Exists(DirectoryPath);
 // 获取文件夹信息,不存在并不会报错
 new DirectoryInfo(DirectoryPath);
 // 获取文件信息,不存在也不会报错
 new FileInfo(FilePath);
  1. 操作文件夹
 DirectoryInfo directoryInfo = Directory.CreateDirectory(LogPath); //一次性创建全部的子路径
 Directory.Move(LogPath, LogMovePath); //移动  原文件夹就不在了
 Directory.Delete(LogMovePath); //删除
  1. 写入文件
 if (!File.Exists(FilePath))
 {
     using (FileStream fileStream = File.Create(FilePath))
     {
         string name = "小春的文档";
         byte[] bytes = Encoding.Default.GetBytes(name);
         fileStream.Write(bytes, 0, bytes.Length);
         fileStream.Flush();
     }
     using (FileStream fileStream = File.Create(FilePath))
     {
         StreamWriter sw = new StreamWriter(fileStream);
         sw.WriteLine("小春的文档");
         sw.Flush();
     }
     using (FileStream sw = File.AppendText(FilePath))
     {
         string msg = "2023年4月25日10:15:47";
         sw.WriteLine(msg);
         sw.Flush();
     }
     using (FileStream sw = File.AppendText(FilePath))
     {
         string name = "小春的文档 |2023年4月25日10:20:01";
         byte[] bytes = Encoding.Default.GetBytes(name);
         sw.BaseStream.Write(bytes, 0, bytes.Length);
         sw.Flush();
     }
 }
  1. 读取文件
 foreach (string result in File.ReadAllLine(FilePath))
 {
     Console.WriteLine(result);
 }
 string sResult = File.ReadAllText(FilePath);
 Byte[] byteContent = File.ReadAllBytes(FilePath);
 string sResultByte = System.Text.Encoding.UTF8.GetString(byteContent);
 ​
 using (FileStream stream = File.OpenRead(FilePath))
 {
     int length = 5;
     int result = 0;
     do 
     {
         byte[] bytes = new byte[length];
         result = stream.Read(bytes, 0, 5);
         for (int i = 0; i < result; i++)
         {
             Console.WriteLine(bytes[i].ToString());
         }
     } while (length == result);
 ​
     File.Copy(FilePath, FilePathCopy);
     File.Move(FilePath, FilePathMove);
     File.Delete(FilePathCopy);
     File.Delete(FilePathMove); // 尽量不要delete
 }
  1. 获取驱动器信息
 DriverInfo[] drives = DriverInfo.GetDrives();
 foreach (DriveInfo drive in drives)
 {
     if (drive.IsReady)
     {
         Console.WriteLine("类型:{0} 卷标:{1} 名称:{2} 总空间:{3} 剩余空间:{4}", drive.DriveType, drive.VolumeLabel, drive.Name, drive.TotalSize, drive.TotalFreeSpace);
     }
     else
     {
         Console.WriteLine("类型:{0}  is not ready", drive.DriveType);
     }
     Console.WriteLine(Path.GetDirectoryName(LogPath));  //返回目录名,需要注意路径末尾是否有反斜杠对结果是有影响的
     Console.WriteLine(Path.GetDirectoryName(@"d:\abc")); //将返回 d:\
     Console.WriteLine(Path.GetDirectoryName(@"d:\abc"));// 将返回 d:\abc
     Console.WriteLine(Path.GetRandomFileName());//将返回随机的文件名
     Console.WriteLine(Path.GetFileNameWithoutExtension("d:\abc.txt"));// 将返回abc
     Console.WriteLine(Path.GetInvalidPathChars());// 将返回禁止在路径中使用的字符
     Console.WriteLine(Path.GetInvalidFileNameChars());//将返回禁止在文件名中使用的字符
     Console.WriteLine(Path.Combine(LogPath, "log.txt"));//合并两个路径
 }
  1. 日志打印
 public static void Log(string msg)
 {
     StreamWriter sw = null;
     try
     {
         string fileName = "log.txt";
         string totalPath = Path.Combine(LogPath, fileName);
 ​
         if (!Directory.Exists(LogPath))
         {
             Directory.CreateDirectory(LogPath);
         }
         sw = File.AppendText(totalPath);
         sw.WriteLine(string.Format("{0}:{1}", DateTime.Now, msg));
         sw.WriteLine("===================================================");
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message); // log
         //throw ex;
         //throw new exception("这里异常");
     }
     finally
     {
         if (sw != null)
         {
             sw.Flush();
             sw.Close();
             sw.Dispose();
         }
     }
 }
  1. 系统自带的序列化器使用
 public class SerializeHelper
 {
     public static void BinarySerialize()
     {
         // 使用二进制序列化对象
         string fileName = Path.Combine(Constant.SerializeDataPath, @"BinarySerialize.txt"); // 文件名称与路径
         using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
         {
             // 需要一个stream,这里是直接写入文件了
             List<Programmer> pList = DataFactory.BuildProgrammerList();
             BinaryFormatter binFormat = new BinaryFormatter(); // 创建二进制序列化器
             binFormat.Serialize(fStream, pList);
         }
         using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
         {
             // 需要一个stream,这里是来源于文件
             BinaryFormatter binFormat = new BinaryFormatter(); // 创建二进制序列化器
             // 使用二进制反序列化对象
             fStream.Position = 0;// 重置流位置
             List<Programmer> pList = (List<Programmer>)binFormat.Deserialize(fStream);// 反序列化对象
         }
     }
 ​
     //BinaryFormatter序列化自定义类的对象时,序列化之后的流中带有空字符,以致于无法反序列化,反序列化时总是报错“在分析完成之前就遇到流结尾”(已经调用了stream.Seek(0, SeekOrigin.Begin);)。
     //改用XmlFormatter序列化之后,可见流中没有空字符,从而解决上述问题,但是要求类必须有无参数构造函数,而且各属性必须既能读又能写,即必须同时定义getter和setter,若只定义getter,则反序列化后的得到的各个属性的值都为null。
 ​
     public static void SoapSerialize()
     {
         //使用Soap序列化对象
         string fileName = Path.Combine(Constant.SerializeDataPath, @"SoapSerialize.txt");//文件名称与路径
         using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
         {
             List<Programmer> pList = DataFactory.BuildProgrammerList();
             SoapFormatter soapFormat = new SoapFormatter();//创建二进制序列化器
             //soapFormat.Serialize(fStream, list);//SOAP不能序列化泛型对象
             soapFormat.Serialize(fStream, pList.ToArray());
         }
         using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
         {
             SoapFormatter soapFormat = new SoapFormatter();//创建二进制序列化器
             //使用二进制反序列化对象
             fStream.Position = 0;//重置流位置
             List<Programmer> pList = ((Programmer[])soapFormat.Deserialize(fStream)).ToList();//反序列化对象
         }
     }
 ​
 ​
     public static void XmlSerialize()
     {
         //使用XML序列化对象
         string fileName = Path.Combine(Constant.SerializeDataPath, @"Student.xml");//文件名称与路径
         using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
         {
             List<Programmer> pList = DataFactory.BuildProgrammerList();
             XmlSerializer xmlFormat = new XmlSerializer(typeof(List<Programmer>));//创建XML序列化器,需要指定对象的类型
             xmlFormat.Serialize(fStream, pList);
         }
         using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
         {
             XmlSerializer xmlFormat = new XmlSerializer(typeof(List<Programmer>));//创建XML序列化器,需要指定对象的类型
             //使用XML反序列化对象
             fStream.Position = 0;//重置流位置
             List<Programmer> pList = pList = (List<Programmer>)xmlFormat.Deserialize(fStream);
         }
     }
 ​
     public static void Json()
     {
         List<Programmer> pList = DataFactory.BuildProgrammerList();
         string result = JsonHelper.ObjectToString<List<Programmer>>(pList);
         List<Programmer> pList1 = JsonHelper.StringToObject<List<Programmer>>(result);
     }
 }
  1. 使用序列化器实现实体序列化
 public class XmlHelper
 {
     public static string ToXml<T>(T t) where T : new()
     {
         Type type = typeof(T);  
         XmlSerializer xmlSerializer = new XmlSerializer(type);
         Stream stream = new MemoryStream();
         xmlSerializer.Serialize(stream, t);
         stream.Position = 0;
         StreamReader reader = new StreamReader(stream);
         string text = reader.ReadToEnd();
         return text;
     }
     public static T ToObject<T>(string content) where T : new()
     {
         using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(content)))
         {
             XmlSerializer xmlFormat = new XmlSerializer(typeof(T));
             return (T)xmlFormat.Deserialize(stream);
         }
     }
     public static T FileToObject<T>(string fileName) where T : new()
     {
         string CurrentXMLPath = ConfigurationManager.AppSettings["CurrentXMLPath"];
         fileName = Path.Combine(CurrentXMLPath, @"Student.xml");
         using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
         {
             XmlSerializer xmlFormat = new XmlSerializer(typeof(T));
             return (T)xmlFormat.Deserialize(fStream);
         }
     }
 }
  1. 自己实现序列化
 public static class xHelper
 {
     /// <summary>   
     /// 实体转化为XML   
     /// </summary>   
     public static string ParseToXml<T>(this T model, string fatherNodeName)
     {
         var xmldoc = new XmlDocument();
         var modelNode = xmldoc.CreateElement(fatherNodeName);
         xmldoc.AppendChild(modelNode);
 ​
         if (model != null)
         {
             foreach (PropertyInfo property in model.GetType().GetProperties())
             {
                 var attribute = xmldoc.CreateElement(property.Name);
                 if (property.GetValue(model, null) != null)
                     attribute.InnerText = property.GetValue(model, null).ToString();
                 //else
                 //    attribute.InnerText = "[Null]";
                 modelNode.AppendChild(attribute);
             }
         }
         return xmldoc.OuterXml;
     }
 ​
     /// <summary>
     /// XML转换为实体,默认 fatherNodeName="body"
     /// </summary>
     /// <typeparam name="T"></typeparam>
     /// <param name="xml"></param>
     /// <param name="fatherNodeName"></param>
     /// <returns></returns>
     public static T ParseToModel<T>(this string xml, string fatherNodeName = "body") where T : class ,new()
     {
         if (string.IsNullOrEmpty(xml))
             return default(T);
         var xmldoc = new XmlDocument();
         xmldoc.LoadXml(xml);
         T model = new T();
         var attributes = xmldoc.SelectSingleNode(fatherNodeName).ChildNodes;
         foreach (XmlNode node in attributes)
         {
             foreach (var property in model.GetType().GetProperties().Where(property => node.Name == property.Name))
             {
                 if (!string.IsNullOrEmpty(node.InnerText))
                 {
                     property.SetValue(model,
                                       property.PropertyType == typeof(Guid)
                                       ? new Guid(node.InnerText)
                                       : Convert.ChangeType(node.InnerText, property.PropertyType));
                 }
                 else
                 {
                     property.SetValue(model, null);
                 }
             }
         }
         return model;
     }
 ​
     /// <summary>
     /// XML转实体
     /// </summary>
     /// <typeparam name="T"></typeparam>
     /// <param name="xml"></param>
     /// <param name="headtag"></param>
     /// <returns></returns>
     public static List<T> XmlToObjList<T>(this string xml, string headtag)
         where T : new()
         {
 ​
             var list = new List<T>();
             XmlDocument doc = new XmlDocument();
             PropertyInfo[] propinfos = null;
             doc.LoadXml(xml);
             XmlNodeList nodelist = doc.SelectNodes(headtag);
             foreach (XmlNode node in nodelist)
             {
                 T entity = new T();
                 if (propinfos == null)
                 {
                     Type objtype = entity.GetType();
                     propinfos = objtype.GetProperties();
                 }
                 foreach (PropertyInfo propinfo in propinfos)
                 {
                     //实体类字段首字母变成小写的  
                     string name = propinfo.Name.Substring(0, 1) + propinfo.Name.Substring(1, propinfo.Name.Length - 1);
                     XmlNode cnode = node.SelectSingleNode(name);
                     string v = cnode.InnerText;
                     if (v != null)
                         propinfo.SetValue(entity, Convert.ChangeType(v, propinfo.PropertyType), null);
                 }
                 list.Add(entity);
 ​
             }
             return list;
         }
 }
  1. JsonHelper
 public class JsonHelper
 {
     #region Json
         /// <summary>
         /// JavaScriptSerializer
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="obj"></param>
         /// <returns></returns>
         public static string ObjectToString<T>(T obj)
     {
         JavaScriptSerializer jss = new JavaScriptSerializer();
         return jss.Serialize(obj);
     }
 ​
     /// <summary>
     /// JavaScriptSerializer
     /// </summary>
     /// <typeparam name="T"></typeparam>
     /// <param name="content"></param>
     /// <returns></returns>
     public static T StringToObject<T>(string content)
     {
         JavaScriptSerializer jss = new JavaScriptSerializer();
         return jss.Deserialize<T>(content);
     }
 ​
     /// <summary>
     /// JsonConvert.SerializeObject
     /// </summary>
     /// <typeparam name="T"></typeparam>
     /// <param name="obj"></param>
     /// <returns></returns>
     public static string ToJson<T>(T obj)
     {
         return JsonConvert.SerializeObject(obj);
     }
 ​
     /// <summary>
     /// JsonConvert.DeserializeObject
     /// </summary>
     /// <typeparam name="T"></typeparam>
     /// <param name="content"></param>
     /// <returns></returns>
     public static T ToObject<T>(string content)
     {
         return JsonConvert.DeserializeObject<T>(content);
     }
     #endregion Json
 }
  1. LinqToXML
 public class LinqToXml
 {
     /// <summary>
     /// 创建XML文件
     /// </summary>
     /// <param name="xmlPath"></param>
     private static void CreateXmlFile(string xmlPath)
     {
         try
         {
             //定义一个XDocument结构
             XDocument myXDoc = new XDocument(
                 new XElement("Users",
                              new XElement("User", new XAttribute("ID", "111111"),
                                           new XElement("name", "EricSun"),
                                           new XElement("password", "123456"),
                                           new XElement("description", "Hello I'm from Dalian")),
                              new XElement("User", new XAttribute("ID", "222222"),
                                           new XElement("name", "Ray"),
                                           new XElement("password", "654321"),
                                           new XElement("description", "Hello I'm from Jilin"))));
             //保存此结构(即:我们预期的xml文件)
             myXDoc.Save(xmlPath);
 ​
             string aa = myXDoc.ToString();
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.ToString());
         }
     }
     /// <summary>
     /// 遍历xml信息
     /// </summary>
     /// <param name="xmlPath"></param>
     private static void GetXmlNodeInformation(string xmlPath)
     {
         try
         {
             //定义并从xml文件中加载节点(根节点)
             XElement rootNode = XElement.Load(xmlPath);
             //XElement rootNode2 = XElement.Parse(xmlPath);
 ​
             //查询语句: 获得根节点下name子节点(此时的子节点可以跨层次:孙节点、重孙节点......)
             IEnumerable<XElement> targetNodes = from target in rootNode.Descendants("name")
                 select target;
             foreach (XElement node in targetNodes)
             {
                 Console.WriteLine("name = {0}", node.Value);
             }
 ​
             //查询语句: 获取ID属性值等于"111111"并且函数子节点的所有User节点(并列条件用"&&"符号连接)
             IEnumerable<XElement> myTargetNodes = from myTarget in rootNode.Descendants("User")
                 where myTarget.Attribute("ID").Value.Equals("111111")
                 && myTarget.HasElements
                 select myTarget;
             foreach (XElement node in myTargetNodes)
             {
                 Console.WriteLine("name = {0}", node.Element("name").Value);
                 Console.WriteLine("password = {0}", node.Element("password").Value);
                 Console.WriteLine("description = {0}", node.Element("description").Value);
             }
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.ToString());
         }
     }
 ​
 ​
     public static void ModifyXmlNodeInformation(string xmlPath)
     {
         try
         {
             //定义并从xml文件中加载节点(根节点)
             XElement rootNode = XElement.Load(xmlPath);
             //查询语句: 获取ID属性值等于"222222"或者等于"777777"的所有User节点(或条件用"||"符号连接)
             IEnumerable<XElement> targetNodes = from target in rootNode.Descendants("User")
                 where target.Attribute("ID").Value == "222222"
                 || target.Attribute("ID").Value.Equals("777777")
                 select target;
             //遍历所获得的目标节点(集合)
             foreach (XElement node in targetNodes)
             {
                 //将description节点的InnerText设置为"Hello, I'm from USA."
                 node.Element("description").SetValue("Hello, I'm from USA.");
             }
             //保存对xml的更改操作
             rootNode.Save(xmlPath);
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.ToString());
         }
     }
 ​
     private static void AddXmlNodeInformation(string xmlPath)
     {
         try
         {
             //定义并从xml文件中加载节点(根节点)
             XElement rootNode = XElement.Load(xmlPath);
             //定义一个新节点
             XElement newNode = new XElement("User", new XAttribute("ID", "999999"),
                                             new XElement("name", "Rose"),
                                             new XElement("password", "456123"),
                                             new XElement("description", "Hello, I'm from UK."));
             //将此新节点添加到根节点下
             rootNode.Add(newNode);
             //Add 在 XContainer 的子内容的末尾添加内容。
             //AddFirst 在 XContainer 的子内容的开头添加内容。
             //AddAfterSelf 在 XNode 后面添加内容。
             //AddBeforeSelf 在 XNode 前面添加内容。
             //保存对xml的更改操作
             rootNode.Save(xmlPath);
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.ToString());
         }
     }
 ​
     private static void DeleteXmlNodeInformation(string xmlPath)
     {
         try
         {
             //定义并从xml文件中加载节点(根节点)
             XElement rootNode = XElement.Load(xmlPath);
             //查询语句: 获取ID属性值等于"999999"的所有User节点
             IEnumerable<XElement> targetNodes = from target in rootNode.Descendants("User")
                 where target.Attribute("ID").Value.Equals("999999")
                 select target;
 ​
             //将获得的节点集合中的每一个节点依次从它相应的父节点中删除
             targetNodes.Remove();
             //保存对xml的更改操作
             rootNode.Save(xmlPath);
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.ToString());
         }
     }
 ​
 }
  1. ImageHelper
 public class ImageHelper
 {
     private static string ImagePath = ConfigurationManager.AppSettings["ImagePath"];
     private static string VerifyPath = ConfigurationManager.AppSettings["ImagePath"];
     //绘图的原理很简单:Bitmap就像一张画布,Graphics如同画图的手,把Pen或Brush等绘图对象画在Bitmap这张画布上
 ​
     /// <summary>
     /// 画验证码
     /// </summary>
     public static void Drawing()
     {
         Bitmap bitmapobj = new Bitmap(100, 100);
         //在Bitmap上创建一个新的Graphics对象
         Graphics g = Graphics.FromImage(bitmapobj);
         //创建绘画对象,如Pen,Brush等
         Pen redPen = new Pen(Color.Red, 8);
         g.Clear(Color.White);
         //绘制图形
         g.DrawLine(redPen, 50, 20, 500, 20);
         g.DrawEllipse(Pens.Black, new Rectangle(0, 0, 200, 100));//画椭圆
         g.DrawArc(Pens.Black, new Rectangle(0, 0, 100, 100), 60, 180);//画弧线
         g.DrawLine(Pens.Black, 10, 10, 100, 100);//画直线
         g.DrawRectangle(Pens.Black, new Rectangle(0, 0, 100, 200));//画矩形
         g.DrawString("我爱北京天安门", new Font("微软雅黑", 12), new SolidBrush(Color.Red), new PointF(10, 10));//画字符串
         //g.DrawImage(
         if (!Directory.Exists(ImagePath))
             Directory.CreateDirectory(ImagePath);
         bitmapobj.Save(ImagePath + "pic1.jpg", ImageFormat.Jpeg);
         //释放所有对象
         bitmapobj.Dispose();
         g.Dispose();
     }
 ​
     public static void VerificationCode()
     {
         Bitmap bitmapobj = new Bitmap(300, 300);
         //在Bitmap上创建一个新的Graphics对象
         Graphics g = Graphics.FromImage(bitmapobj);
         g.DrawRectangle(Pens.Black, new Rectangle(0, 0, 150, 50));//画矩形
         g.FillRectangle(Brushes.White, new Rectangle(1, 1, 149, 49));
         g.DrawArc(Pens.Blue, new Rectangle(10, 10, 140, 10), 150, 90);//干扰线
         string[] arrStr = new string[] { "我", "们", "孝", "行", "白", "到", "国", "中", "来", "真" };
         Random r = new Random();
         int i;
         for (int j = 0; j < 4; j++)
         {
             i = r.Next(10);
             g.DrawString(arrStr[i], new Font("微软雅黑", 15), Brushes.Red, new PointF(j * 30, 10));
         }
         bitmapobj.Save(Path.Combine(VerifyPath, "Verif.jpg"), ImageFormat.Jpeg);
         bitmapobj.Dispose();
         g.Dispose();
     }
 ​
     /// <summary>
     /// 按比例缩放,图片不会变形,会优先满足原图和最大长宽比例最高的一项
     /// </summary>
     /// <param name="oldPath"></param>
     /// <param name="newPath"></param>
     /// <param name="maxWidth"></param>
     /// <param name="maxHeight"></param>
     public static void CompressPercent(string oldPath, string newPath, int maxWidth, int maxHeight)
     {
         Image _sourceImg = Image.FromFile(oldPath);
         double _newW = (double)maxWidth;
         double _newH = (double)maxHeight;
         double percentWidth = (double)_sourceImg.Width > maxWidth ? (double)maxWidth : (double)_sourceImg.Width;
 ​
         if ((double)_sourceImg.Height * (double)percentWidth / (double)_sourceImg.Width > (double)maxHeight)
         {
             _newH = (double)maxHeight;
             _newW = (double)maxHeight / (double)_sourceImg.Height * (double)_sourceImg.Width;
         }
         else
         {
             _newW = percentWidth;
             _newH = (percentWidth / (double)_sourceImg.Width) * (double)_sourceImg.Height;
         }
         System.Drawing.Image bitmap = new System.Drawing.Bitmap((int)_newW, (int)_newH);
         Graphics g = Graphics.FromImage(bitmap);
         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
         g.Clear(Color.Transparent);
         g.DrawImage(_sourceImg, new Rectangle(0, 0, (int)_newW, (int)_newH), new Rectangle(0, 0, _sourceImg.Width, _sourceImg.Height), GraphicsUnit.Pixel);
         _sourceImg.Dispose();
         g.Dispose();
         bitmap.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg);
         bitmap.Dispose();
     }
 ​
     /// <summary>
     /// 按照指定大小对图片进行缩放,可能会图片变形
     /// </summary>
     /// <param name="oldPath"></param>
     /// <param name="newPath"></param>
     /// <param name="newWidth"></param>
     /// <param name="newHeight"></param>
     public static void ImageChangeBySize(string oldPath, string newPath, int newWidth, int newHeight)
     {
         Image sourceImg = Image.FromFile(oldPath);
         System.Drawing.Image bitmap = new System.Drawing.Bitmap(newWidth, newHeight);
         Graphics g = Graphics.FromImage(bitmap);
         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
         g.Clear(Color.Transparent);
         g.DrawImage(sourceImg, new Rectangle(0, 0, newWidth, newHeight), new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), GraphicsUnit.Pixel);
         sourceImg.Dispose();
         g.Dispose();
         bitmap.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg);
         bitmap.Dispose();
     }
 ​
 ​
     //添加水印
     //二维码 QRCode
 }