.netcore 圖片處理數組
/// <summary> /// 根據文件類型和文件名返回新路徑 /// </summary> /// <param name="type">文件類型</param> /// <param name="fileName">文件名/回傳新的相對路徑</param> /// <returns>全新文件絕對路徑</returns> public static string CreatePath(FileType type, ref string fileName) { string path1 = $"{RootPath}{Enum.GetName(typeof(FileType), type)}/"; var path = $"{AppContext.BaseDirectory}{path1}"; //檢查上傳的物理路徑是否存在,不存在則建立 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string name = $"{DateTime.Now:yyyyMMddHHmmssff}.{GetFileExt(fileName)}"; fileName = $"{path1}{name}"; return $"{path}{name}"; } /// <summary> /// 取小寫文件名後綴 /// </summary> /// <param name="name">文件名</param> /// <returns>返回小寫後綴,不帶「.」</returns> public static string GetFileExt(string name) { return name.Split(".").Last().ToLower(); } /// <summary> /// 是否爲圖片文件 /// </summary> /// <param name="fileExt">文件擴展名,不含「.」</param> public static bool IsImage(string fileExt) { ArrayList al = new ArrayList{"bmp","jpeg","jpg","gif","png","ico"}; return al.Contains(fileExt); } /// <summary> /// 檢查是否容許文件 /// </summary> /// <param name="fileExt">文件後綴</param> /// <param name="allowExt">容許文件數組</param> public static bool CheckFileExt(string fileExt, string[] allowExt) { return allowExt.Any(t => t == fileExt); } /// <summary> /// 製做縮略圖 /// </summary> /// <param name="original">圖片對象</param> /// <param name="newFileName">新圖路徑</param> /// <param name="maxWidth">最大寬度</param> /// <param name="maxHeight">最大高度</param> public static void ThumbImg(Image original, string newFileName, int maxWidth, int maxHeight) { Size newSize = ResizeImage(original.Width, original.Height, maxWidth, maxHeight); using (Image displayImage = new Bitmap(original, newSize)) { try { displayImage.Save(newFileName, original.RawFormat); } finally { original.Dispose(); } } } /// <summary> /// 製做縮略圖 /// </summary> /// <param name="fileName">文件名</param> /// <param name="newFileName">新圖路徑</param> /// <param name="maxWidth">最大寬度</param> /// <param name="maxHeight">最大高度</param> public static void ThumbImg(string fileName, string newFileName, int maxWidth, int maxHeight) { byte[] imageBytes = File.ReadAllBytes(fileName); Image img = Image.FromStream(new MemoryStream(imageBytes)); ThumbImg(img, newFileName, maxWidth, maxHeight); } /// <summary> /// 計算新尺寸 /// </summary> /// <param name="width">原始寬度</param> /// <param name="height">原始高度</param> /// <param name="maxWidth">最大新寬度</param> /// <param name="maxHeight">最大新高度</param> /// <returns></returns> private static Size ResizeImage(int width, int height, int maxWidth, int maxHeight) { if (maxWidth <= 0) maxWidth = width; if (maxHeight <= 0) maxHeight = height; decimal MAX_WIDTH = maxWidth; decimal MAX_HEIGHT = maxHeight; decimal ASPECT_RATIO = MAX_WIDTH / MAX_HEIGHT; int newWidth, newHeight; decimal originalWidth = width; decimal originalHeight = height; if (originalWidth > MAX_WIDTH || originalHeight > MAX_HEIGHT) { decimal factor; if (originalWidth / originalHeight > ASPECT_RATIO) { factor = originalWidth / MAX_WIDTH; newWidth = Convert.ToInt32(originalWidth / factor); newHeight = Convert.ToInt32(originalHeight / factor); } else { factor = originalHeight / MAX_HEIGHT; newWidth = Convert.ToInt32(originalWidth / factor); newHeight = Convert.ToInt32(originalHeight / factor); } } else { newWidth = width; newHeight = height; } return new Size(newWidth, newHeight); } /// <summary> /// 獲得圖片格式 /// </summary> /// <param name="name">文件名稱</param> /// <returns></returns> public static ImageFormat GetFormat(string name) { string ext = GetFileExt(name); switch (ext) { case "ico": return ImageFormat.Icon; case "bmp": return ImageFormat.Bmp; case "png": return ImageFormat.Png; case "gif": return ImageFormat.Gif; default: return ImageFormat.Jpeg; } }
public async Task<string> Upload(IFormFile file, FileType type) { string name = file.FileName; string filePath = FileBase.CreatePath(type, ref name); string ext = FileBase.GetFileExt(file.FileName); bool isDel = false; using (var stream = new FileStream(filePath, FileMode.CreateNew)) { await file.CopyToAsync(stream); if (!FileBase.IsImage(ext)) return name; //若是圖片寬高超出限制則縮小 Image img = Image.FromStream(stream); if (img.Width > Config.ImgMaxWidth || img.Height > Config.ImgMaxHeight) { string path = FileBase.CreatePath(type, ref name); FileBase.ThumbImg(img, path, Config.ImgMaxWidth, Config.ImgMaxHeight); isDel = true; } } if(isDel) File.Delete(filePath); return name; }