调整整个图像文件夹的大小

今天早上,我给了我的各种演示的100多个屏幕截图文件夹。188金宝搏beat体育官网我需要这些来更新我们的网站。不幸的是,它们中的大多数对于该页面来说太大了,需要调整大小。我迅速创建了一个.NET Core Console应用程序来为我修复图像大小。我能够编写代码,调整图像大小,然后在上午10:00之前写博客。代码在下面。享受!

使用LeadTools;使用LeadTools.Codecs;使用LeadTools.imageProcessing;使用系统;使用System.io;名称空间batchresize_netcore {内部类程序{private static void main(string [] args){if(!setlicense())return;// todo:根据需要更改这些const字符串filter = @“*。png”;const String sourceFolder = @“ d:temptemp∖temp readme屏幕截图∖”;var DirectoryInfo = new DirectoryInfo(sourceFolder);foreach(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 // //m.ahtuanjie.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; } } }

关于

开发人员倡导者

    在上面找到有关我的更多信息:
  • LinkedIn
  • 推特
  • YouTube
此条目已发布在图像处理。书签永久链接

发表评论

您的电子邮件地址不会被公开。必需的地方已做标记*