/// <summary> /// 上传图片 /// </summary> /// <param name="name"></param> /// <param name="imageByte"></param> /// <returns></returns> public string UpLoadImage(string name, byte[] imageByte) { try { //事务 using (RgFx.Data.DbTransactionScope scop = new RgFx.Data.DbTransactionScope()) { using (MemoryStream stream = new MemoryStream(imageByte)) { //保存路径 //string temps = HttpContext.Current.Server.MapPath("Images"); string temps = System.Web.Hosting.HostingEnvironment.MapPath("~/WCFServiceImages/" + name + ""); #region//二进制流转Image类型,(加水印) 第一种保存方式 System.Drawing.Image bgimage = System.Drawing.Image.FromStream(stream); stream.Close(); //重新画,加水印 string addText = DateTime.Now.ToString(); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bgimage); System.Drawing.Font f = new System.Drawing.Font("Verdana", 10); System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue); g.DrawString(addText, f, b, 0, 0); g.Dispose(); bgimage.Save(temps); bgimage.Dispose(); #endregion #region//(不加水印把二进制流保存成文件) 第二种保存方式 using (FileStream fstream = File.Create(temps, imageByte.Length)) { fstream.Write(imageByte, 0, imageByte.Length); //二进制转换成文件 } #endregion } //提交事务 scop.Complete(); } return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(new { Statue = "OK", Message = "图片上传成功!" }); } catch (Exception ex) { return ex.ToString(); } }