LEADTOOLS使用教程:调整整个图像文件夹的大小
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微信公众号,及时获取最新动态及最新资讯


QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线