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; using System.Windows.Media; using XCommon.Log; namespace XCommon.file { /// /// 导出文件 /// public class ImportFile { /// /// 下载服务器文件至客户端 /// /// 被下载的文件地址 /// 另存放的目录 public static bool Download(string uri, string savePath) { string fileName; //被下载的文件名 if (uri.IndexOf("\\") > -1) { fileName = uri.Substring(uri.LastIndexOf("\\") + 1); } else { fileName = uri.Substring(uri.LastIndexOf("/") + 1); } if (!savePath.EndsWith("/") && !savePath.EndsWith("\\")) { savePath = savePath + "/"; } savePath += DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + fileName; //另存为的绝对路径+文件名 WebClient client = new WebClient(); try { client.DownloadFile(uri, savePath); } catch (Exception ex) { LoggerHelper.ErrorLog($"ImportFile.Download 失败:{ex.Message}"); return false; } return true; } /// /// 获取图片 /// /// 图片路径 /// public static ImageSource GetImage(string filePath) { byte[] imageBytes = Convert.FromBase64String(filePath); // 读入MemoryStream对象 MemoryStream ms = new System.IO.MemoryStream(imageBytes); ImageSourceConverter imageSourceConverter = new ImageSourceConverter(); // 转成图片 ImageSource source = (ImageSource)imageSourceConverter.ConvertFrom(ms); return source; } /// /// 获取图片Base64字符串 /// /// 图片路径 /// public static string GetImageByString(string filePath) { Bitmap picture = new Bitmap(filePath); MemoryStream ms = new MemoryStream(); picture.Save(ms, System.Drawing.Imaging.ImageFormat.Png); byte[] arr = new byte[ms.Length]; ms.Position = 0; ms.Read(arr, 0, (int)ms.Length); ms.Close(); string pic = Convert.ToBase64String(arr); return pic; } } }