using Face.Domain.ViewModels; using Face.Services.Manager; using Face.Web.Areas.App.Controllers; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Threading.Tasks; using System.Web; using ImageMagick; using System.Web.Hosting; using static WebSocketToolsConsole.Entity; namespace Face.Web.Areas.App.Models { /// /// 图片处理类 /// public class practical { /// /// 网络图片保存到项目目录 /// /// /// public string Down(string url, string dtnow) { try { WebRequest wreq = WebRequest.Create(url); HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse(); Stream s = wresp.GetResponseStream(); System.Drawing.Image img; img = System.Drawing.Image.FromStream(s); string TimePath = HostingEnvironment.MapPath("~/Upload/Image/");//保存路径 string TimePathMake = HostingEnvironment.MapPath("~/Upload/Img/");//小图 img.Save(TimePath + dtnow + "_1" + ".jpg", ImageFormat.Jpeg); Task res = MakeThumbnail(img, TimePathMake, 500, 500, "", dtnow); //System.IO.File.Delete(TimePath + "1" + name + ".jpg"); res.Start(); //MemoryStream ms = new MemoryStream(); img.Dispose(); string ftpURL = "auth.blv-oa.com:50/uts_Manager/oyl_FTP/Image"; //string ftpURLTwo = "auth.blv-oa.com:50/uts_Manager/oyl_FTP/Img"; //FtpUploadShow(TimePathMake + dtnow + ".jpg", ftpURLTwo); return FtpUploadShow(res.Result, ftpURL); } catch (Exception ex) { LogHelp.WriteExceptionLog(ex, "图片"); return "图片路径错误"; } } public Task MakeThumbnail(Image originalImage, string thumbnailPath, int width, int height, string mode, string name) { ReturnResult result = new ReturnResult(); int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; if (mode == "") { if (ow > oh) { mode = "W"; } else { mode = "H"; } } switch (mode) { case "HW"://指定高宽缩放(可能变形) break; case "W"://指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case "H"://指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case "Cut"://指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; default: break; } //新建一个bmp图片 Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板 Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); try { if (!Directory.Exists(thumbnailPath)) { Directory.CreateDirectory(thumbnailPath); } //以jpg格式保存缩略图 bitmap.Save(thumbnailPath + "2" + name + ".jpg"); ImgServerController.CompressImage(thumbnailPath + "2" + name + ".jpg", thumbnailPath + name + ".jpg"); //System.IO.File.Delete(thumbnailPath + "2" + name + ".jpg"); return new Task(() => thumbnailPath + name + ".jpg"); //return thumbnailPath + info.Name; } catch (System.Exception e) { LogHelp.WriteExceptionLog(e, this.GetType().Name); return null; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } } public string FtpUploadShow(string filename, string ftpServerIP) { //filename += ".jpg"; string ftpUserID = "uts_manager";//ftp账号 string ftpPassword = "uts_Inhaos@all";//ftp密码 FileInfo fileInf = new FileInfo(filename); FtpWebRequest reqFTP; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate); try { // 根据uri创建FtpWebRequest对象 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name)); string s = fileInf.Name; // ftp用户名和密码 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); // 默认为true,连接不会被关闭 // 在一个命令之后被执行 reqFTP.KeepAlive = true; //如果要连接的 FTP 服务器要求凭据并支持安全套接字层 (SSL),则应将 EnableSsl 设置为 true。如果不写会报出421错误(服务不可用) reqFTP.EnableSsl = false; // 指定执行什么命令 reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // 指定数据传输类型 reqFTP.UseBinary = true; // 上传文件时通知服务器文件的大小 reqFTP.ContentLength = fileInf.Length; // 缓冲大小设置为 10kb int buffLength = 10480; byte[] buff = new byte[buffLength]; int contentLen; // 打开一个文件流 (System.IO.FileStream) 去读上传的文件 FileStream fs = fileInf.OpenRead(); try { // 把上传的文件写入流 Stream strm = reqFTP.GetRequestStream(); // 每次读文件流的buffLength kb contentLen = fs.Read(buff, 0, buffLength); // 流内容没有结束 while (contentLen != 0) { // 把内容从file stream 写入 upload stream strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } // 关闭两个流 strm.Close(); fs.Close(); return fileInf.Name; } catch (Exception ex) { LogHelp.WriteExceptionLog(ex,"图片ftp错误"+ex.Message); } } catch (Exception e) { LogHelp.WriteExceptionLog(e, "图片ftp错误" + e.Message); } return null; } public static bool ValidateServerCertificate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; } } }