using Api.Framework.Tools; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace TBAppraisalTools { class Tools { /// /// 把directory文件夹里的png文件生成为gif文件,放在giffile /// /// 图片的数组 /// gif保存路径 /// 每帧的时间/ms /// 是否重复 public static string PngsToGif(string[] files, string giffile, int time = 0, bool repeat = false) { try { //一般文件名按顺序排 AnimatedGifEncoder e = new AnimatedGifEncoder(); e.Start(giffile); //每帧播放时间 e.SetDelay(time); //1:不重复,0:重复 e.SetRepeat(repeat ? 0 : -1); for (int i = 0, count = files.Length; i < count; i++) { if (!string.IsNullOrEmpty(files[i]) && File.Exists(files[i])) e.AddFrame(Tools.ReadImageFile(files[i])); } e.Finish(); return giffile; } catch (Exception ex) { throw ex; } } /// /// 按比例缩放图片 /// /// 图片路径 /// 宽度 /// 高度 /// public static Image ZoomPicture(Image SourceImage, int TargetWidth, int TargetHeight) { int IntWidth; //新的图片宽 int IntHeight; //新的图片高 try { System.Drawing.Imaging.ImageFormat format = SourceImage.RawFormat; Bitmap SaveImage = new Bitmap(TargetWidth, TargetHeight); Graphics g = Graphics.FromImage(SaveImage); g.Clear(Color.White); if (SourceImage.Width > TargetWidth && SourceImage.Height <= TargetHeight)//宽度比目的图片宽度大,长度比目的图片长度小 { IntWidth = TargetWidth; IntHeight = (IntWidth * SourceImage.Height) / SourceImage.Width; } else if (SourceImage.Width <= TargetWidth && SourceImage.Height > TargetHeight)//宽度比目的图片宽度小,长度比目的图片长度大 { IntHeight = TargetHeight; IntWidth = (IntHeight * SourceImage.Width) / SourceImage.Height; } else if (SourceImage.Width <= TargetWidth && SourceImage.Height <= TargetHeight) //长宽比目的图片长宽都小 { IntHeight = SourceImage.Width; IntWidth = SourceImage.Height; } else//长宽比目的图片的长宽都大 { IntWidth = TargetWidth; IntHeight = (IntWidth * SourceImage.Height) / SourceImage.Width; if (IntHeight > TargetHeight)//重新计算 { IntHeight = TargetHeight; IntWidth = (IntHeight * SourceImage.Width) / SourceImage.Height; } } g.DrawImage(SourceImage, (TargetWidth - IntWidth) / 2, (TargetHeight - IntHeight) / 2, IntWidth, IntHeight); SourceImage.Dispose(); return SaveImage; } catch (Exception ex) { } return null; } /// /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件 /// /// /// public static Bitmap ReadImageFile(string path) { FileStream fs = File.OpenRead(path); //OpenRead int filelength = 0; filelength = (int)fs.Length; //获得文件长度 Byte[] image = new Byte[filelength]; //建立一个字节数组 fs.Read(image, 0, filelength); //按字节流读取 Image result = Image.FromStream(fs); fs.Close(); Bitmap bit = new Bitmap(result); return bit; } /// /// 下载网络图片 /// /// 图片的链接 /// 下载图片保存的路径 /// public static void DownloadImage(string url, string path) { HttpWebResponse rsp = null; try { url = (url.StartsWith("http") ? url : ("http:" + url)); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.ServicePoint.Expect100Continue = false; req.Method = "GET"; req.KeepAlive = true; req.ContentType = "image/*"; rsp = (HttpWebResponse)req.GetResponse(); } catch (Exception ex) { throw ex; } Stream stream = null; try { // 以字符流的方式读取HTTP响应 stream = rsp.GetResponseStream(); Image.FromStream(stream).Save(path); } catch { if (stream != null) stream.Close(); if (rsp != null) rsp.Close(); } finally { // 释放资源 if (stream != null) stream.Close(); if (rsp != null) rsp.Close(); } } /// /// 图片放大缩小 变形操作 /// /// /// /// /// public static Bitmap CutEllipse(Image img, Rectangle rec, Size size) { Bitmap bitmap = new Bitmap(size.Width, size.Height); using (Graphics g = Graphics.FromImage(bitmap)) { using (TextureBrush br = new TextureBrush(img, System.Drawing.Drawing2D.WrapMode.Clamp, rec)) { br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.FillEllipse(br, new Rectangle(Point.Empty, size)); //g.FillRectangle(br, new Rectangle(Point.Empty, size)); } } return bitmap; } /// /// 这方法将以前的地址更新成新的目录地址(防止用户更改了软件目录) /// /// 之前完整的路径 /// 查找的节点(文件名前的那一段内容) /// 软件的目录 /// public static string RefreshFileAddress(string currentFile, string node, string newPath) { var index = currentFile.IndexOf(node); var imageNick = currentFile.Remove(0, index + node.Length);//获取图片名和后缀 return Util.MapFile(imageNick, newPath);//当前项目的完成图片路径(以上操作是为了怕用户转移程序路径) } /// /// 更新文本格式,优惠券模板中label超出背景部分自动换行(比如标题过长) /// /// 总背景的宽 /// 文本的X坐标(起点) /// 文本内容 /// 格式化后的文本真正的宽 /// public static string RefreshTextFormat(int totalWidth, int startPoint, string srcText, float tWidth) { var surLocation = totalWidth - startPoint; if (surLocation < tWidth) { var nuit = Math.Ceiling(tWidth / srcText.Length);//一个字的宽度 var rowNum = (int)Math.Ceiling(surLocation / nuit);//大约一列最多能放几个字 double rows = srcText.Length / rowNum;//行数 int count = (int)Math.Ceiling(rows); for (var i = 1; i <= count; i++) { srcText = srcText.Insert(rowNum * i, "\r\n"); } } return srcText; } /// /// 通过路径删除所有的文件,以及文件夹 /// /// public static void DelectDir(string srcPath) { DirectoryInfo dir = new DirectoryInfo(srcPath); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录 foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判断是否文件夹 { DirectoryInfo subdir = new DirectoryInfo(i.FullName); subdir.Delete(true); //删除子目录和文件 } else { File.Delete(i.FullName); //删除指定文件 } } } } }