翻譯|使用教程|編輯:莫成敏|2019-08-28 15:19:37.030|閱讀 361 次
概述:本文主要介紹整個文件夾中的圖像尺寸都太大了時,如何調整整個圖像文件夾的大小。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
LEADTOOLS (Lead Technology)由Moe Daher and Rich Little創建于1990年,其總部設在北卡羅來納州夏洛特。LEAD的建立是為了使Daher先生在數碼圖象與壓縮技術領域的發明面向市場。在過去超過18年的發展歷程中,LEAD以其在全世界主要國家中占有的市場領導地位,在數碼圖象開發工具領域中已成為既定的全球領導者。LEADTOOLS開發與發布的LEAD是屢獲殊榮的開發工具包。
本文主要介紹整個文件夾中的圖像尺寸都太大了時,如何調整整個圖像文件夾的大小。看看具體怎么操作吧,感興趣的朋友不妨自己動手試一試~
今天早上我收到了一個包含100多張各種演示截圖的文件夾。我需要這些來更新我們的網站。不幸的是,它們中的大多數對于頁面而言都太大了,需要調整大小。我快速創建了一個.NET Core控制臺應用程序來為我修復圖像大小。我編寫代碼,快速調整圖像大小。代碼如下,大家請享用吧!
using Leadtools;using Leadtools.Codecs;using Leadtools.ImageProcessing;using System;using System.IO;namespace BatchResize_NetCore{ internal class Program { private static void Main(string[] args) { if (!SetLicense()) return; // TODO: Change these as needed const string filter = @"*.png"; const string sourceFolder = @"D:?temp?readme screenshots?"; var directoryInfo = new DirectoryInfo(sourceFolder); foreach (var file in directoryInfo.GetFiles(filter)) { ShowMessage($"Processing {file.Name}"); ResizeImage(file.FullName); } ShowMessage("Processing complete."); } public static void ResizeImage(string sourcePath, string destinationPath = null) { const float max = 500f; // The max width or height if (destinationPath == null) destinationPath = sourcePath; LeadSize CalculateNewSize(RasterImage i) { var ratio = i.Width > i.Height ? max / i.Width : max / i.Height; return new LeadSize((int)(i.Width * ratio), (int)(i.Height * ratio)); } using (var codecs = new RasterCodecs()) using (var image = codecs.Load(sourcePath)) { // 9 is slower but results in small file size // //www.leadtools.com/help/leadtools/v20/dh/co/codecspngsaveoptions-qualityfactor.html codecs.Options.Png.Save.QualityFactor = 9; if (image.Width <= 500 && image.Height <= max) return; // does not need to be resized var newSize = CalculateNewSize(image); new SizeCommand { Width = newSize.Width, Height = newSize.Height, Flags = RasterSizeFlags.Bicubic | RasterSizeFlags.ScaleToGray }.Run(image); codecs.Save(image, destinationPath, RasterImageFormat.Png, image.BitsPerPixel); } } public static bool SetLicense() { try { // TODO: Replace these with the path to the LEADTOOLS license file const string licenseFilePath = null; var developerKey = null; if (developerKey != null) RasterSupport.SetLicense(licenseFilePath, developerKey); } catch (Exception ex) { Console.WriteLine(ex.Message); } return !RasterSupport.KernelExpired; } public static void ShowMessage(string message, bool error = false) { var origColor = Console.ForegroundColor; Console.ForegroundColor = error ? ConsoleColor.Red : ConsoleColor.Green; Console.WriteLine(message); Console.ForegroundColor = origColor; } }}
這些代碼快速解決文件夾中圖像的大小,節省那您的時間~
LEADTOOLS Document Imaging Developer Toolkit是多語言的文檔圖像處理控件,支持光符識別處理、條形碼掃描識別等。其主要功能包括綜合圖像注釋,專業的黑白圖像顯示(例如灰度級和偏黑),以及專業的黑白圖像處理。其它功能包括對黑白圖像的性能和內存進行優化,文檔圖像清理(包括倒置文本,去邊框)以及使用LEADTOOLS Fast TWAIN和WIA進行掃描。
想要購買該產品正版授權,或了解更多產品信息請點擊
掃描關注慧聚IT微信公眾號,及時獲取最新動態及最新資訊
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn