开源免费的.NET图像即时处理的组件ImageProcessor
?
承接以前的組件系列,這個(gè)組件系列旨在介紹.NET相關(guān)的組件,讓大家可以在項(xiàng)目中有一個(gè)更好的選擇,社區(qū)對(duì)于第三方插件的介紹還是比較少的,很多博文的內(nèi)容主要還是介紹一些簡(jiǎn)單的操作(很多人都說(shuō)博客園現(xiàn)在是“hello world”的水平,博文質(zhì)量在下降,對(duì)于這一說(shuō)法,我覺(jué)得這個(gè)說(shuō)法有些過(guò)頭了,可能一些博文的確寫(xiě)的比較初級(jí),但是很多博文的深入還是比較大,只是很少受人關(guān)注),這個(gè)組件系列主要在介紹一些功能的組件,附帶該組件的核心對(duì)象介紹。
? ?組件的介紹絕對(duì)不是一篇文章可以敘述完的,因?yàn)橐粋€(gè)組件是經(jīng)過(guò)開(kāi)發(fā)者很長(zhǎng)周期的開(kāi)發(fā),絕不是我這里一篇簡(jiǎn)單的博文就可以介紹完畢的,組件介紹的系列,一般會(huì)沿襲著組件背景介紹、組件使用介紹、核心對(duì)象介紹等等內(nèi)容。如果對(duì)組件感興趣,可以深入的了解和學(xué)習(xí)。
? ?廢話少說(shuō),進(jìn)入正題。
? ?我們?cè)陧?xiàng)目中很多時(shí)候都會(huì)對(duì)文件進(jìn)行處理,例如文件的上傳下載等等。其中對(duì)圖片的實(shí)時(shí)操作也會(huì)較多,在這里介紹一款用C#編寫(xiě)的輕量級(jí)庫(kù)的集合,它允許你使用.NET 4.5+來(lái)動(dòng)態(tài)地處理圖像的組件,那就是ImageProcessor,用于圖像的即時(shí)處理的.NET庫(kù)。(組織的開(kāi)源項(xiàng)目組,會(huì)經(jīng)過(guò)第一個(gè)項(xiàng)目的磨合后,第二項(xiàng)目會(huì)開(kāi)發(fā)一個(gè).NET Core組件)
一.ImageProcessor組件概述
? ??ImageProcessor是用C#編寫(xiě)的輕量級(jí)庫(kù)的集合,它允許你使用.NET 4.5+來(lái)動(dòng)態(tài)地處理圖像,包括兩個(gè)主庫(kù)ImageProcessor(用于桌面和應(yīng)用程序使用)ImageProcessor.Web(ASP.NET構(gòu)建的動(dòng)態(tài)圖像處理擴(kuò)展),該組件快速,可擴(kuò)展,易于使用,捆綁了一些很強(qiáng)大的功能,而且是完全開(kāi)源。該組件有兩個(gè)部分,我們今天將主要講解ImageProcessor部分的內(nèi)容,如果對(duì)另外一個(gè)感興趣,可以自行了解。
? ?ImageProcessor.Web向項(xiàng)目添加了一個(gè)可配置的HttpModule,允許對(duì)圖像文件進(jìn)行即時(shí)處理。該模塊還提供了一個(gè)基于文件和瀏覽器的緩存,可以處理數(shù)百萬(wàn)的圖像,增加處理輸出和節(jié)省寶貴的服務(wù)器內(nèi)存。該組件的功能方法包括:調(diào)整大小,旋轉(zhuǎn),圓角,翻轉(zhuǎn),裁剪,水印,過(guò)濾器,飽和度,亮度,對(duì)比度,質(zhì)量,格式,小插曲,高斯模糊,高斯銳化和透明度。
? ?ImageProcessor.Web的當(dāng)前版本是4.8.2,可以下載源碼和DLL文件(本人建議最好下載源碼,好處就不在這里贅述)。
? ?ImageProcessor.Web是ImageProcessor的Web擴(kuò)展,允許開(kāi)發(fā)人員使用Url API的查詢(xún)字符串參數(shù)作為指令執(zhí)行圖像操作。此過(guò)程的輸出是高度優(yōu)化的網(wǎng)絡(luò),以確保web項(xiàng)目較高的性能。安裝ImageProcessor.Web時(shí),默認(rèn)情況下,Web.config中添加配置節(jié)點(diǎn),如下節(jié)點(diǎn)。
<add name="ImageProcessorModule" type="ImageProcessor.Web.HttpModules.ImageProcessingModule, ImageProcessor.Web" /></httpModules>? ? 這允許庫(kù)ImageProcessingModule攔截本地圖像處理請(qǐng)求。ImageProcessor.Web是高度可配置的。可以將其他配置文件添加到解決方案中,以便從多個(gè)來(lái)源檢索,處理和緩存圖像。對(duì)于該組件的配置設(shè)置可以查看文檔。
二.ImageProcessor組件操作概述
? 介紹了組件的相關(guān)信息,在這里介紹一下該組件的操作實(shí)例。ImageFactory類(lèi)提供了對(duì)給定圖像執(zhí)行各種操作功能的方法。它經(jīng)過(guò)精心設(shè)計(jì)以防止在以高性能方式處理圖像時(shí)通常發(fā)生的各種內(nèi)存泄漏。這使其可以安全地在桌面和Web環(huán)境中使用。ImageFactory自動(dòng)檢測(cè)給定圖像的正確文件類(lèi)型,并且該類(lèi)的API是流暢的,這允許您輕松地鏈接方法以提供所需的輸出。例如,以下代碼加載,調(diào)整大小,設(shè)置新格式并保存包含圖像信息的MemoryStream。
public static void Image(string file)
? ? ? ? {
? ? ? ? ? ? if (string.IsNullOrEmpty(file))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? throw new ArgumentNullException(file);
? ? ? ? ? ? }
? ? ? ? ? ? byte[] photoBytes = System.IO.File.ReadAllBytes(file);
? ? ? ? ? ? // 檢測(cè)格式
? ? ? ? ? ? ISupportedImageFormat format = new JpegFormat { Quality = 70 };
? ? ? ? ? ? Size size = new Size(150, 0);
? ? ? ? ? ? using (MemoryStream inStream = new MemoryStream(photoBytes))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? using (MemoryStream outStream = new MemoryStream())
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? // 使用重載初始化ImageFactory以保留EXIF元數(shù)據(jù)。
? ? ? ? ? ? ? ? ? ? using (ImageFactory imageFactory = new ImageFactory(true))
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? // 加載,調(diào)整大小,設(shè)置格式和質(zhì)量并保存圖像。
? ? ? ? ? ? ? ? ? ? ? ? imageFactory.Load(inStream)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .Resize(size)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .Format(format)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .Save(outStream);
? ? ? ? ? ? ? ? ? ? ? ? //對(duì)獲取的imageFactory對(duì)象進(jìn)行對(duì)應(yīng)的操作
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ?//對(duì)獲取的數(shù)據(jù)流進(jìn)行操作
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
對(duì)于圖片的操作,具體有較多的操作方式,具體的方法有如下的方法:
| 方法名稱(chēng) | 方法操作說(shuō)明 |
| Reset | 將當(dāng)前圖像重置為其原始加載狀態(tài) |
| Alpha | 更改當(dāng)前圖像的不透明度 |
| AutoRotate | ?執(zhí)行自動(dòng)旋轉(zhuǎn)以確保反映EXIF定義的旋轉(zhuǎn)最終圖像 |
| BitDepth | 改變當(dāng)前圖像的位深度 |
| Brightness | 更改當(dāng)前圖像的亮度 |
| BackgroundColor | 更改當(dāng)前圖像的背景顏色 |
| Constrain | 約束當(dāng)前圖像,調(diào)整其大小以適合給定的尺寸,同時(shí)保持其縱橫比 |
| Contrast | 更改當(dāng)前圖像的對(duì)比度 |
| Crop | 將當(dāng)前圖像裁剪到給定的位置和大小 |
| DetectEdges | 檢測(cè)當(dāng)前圖像中的邊緣 |
| Resolution | 設(shè)置圖像的分辨率 |
| EntropyCrop | 將圖像修剪到最大熵的區(qū)域 |
| Filter | 將過(guò)濾器應(yīng)用于當(dāng)前圖像 |
| Flip | 水平或垂直翻轉(zhuǎn)當(dāng)前圖像 |
| Gamma | 調(diào)整給定圖像的灰度(光強(qiáng)度)分量 |
| GaussianBlur | 使用高斯內(nèi)核模糊當(dāng)前圖像 |
| Hue | 改變當(dāng)前圖像的色調(diào),改變整體顏色 |
| Halftone | 將當(dāng)前圖像轉(zhuǎn)換為該圖像的CMYK半色調(diào)表示 |
| Quality | 改變當(dāng)前圖像的輸出質(zhì)量 |
| ReplaceColor | 替換當(dāng)前圖像中的顏色 |
| Resize | 將當(dāng)前圖像調(diào)整為給定尺寸 |
| Rotate | 將當(dāng)前圖像旋轉(zhuǎn)給定角度 |
? ? 以上只是列出了一些主要的操作方法,還有其他的方法這里就不再介紹,有興趣可以自己取實(shí)踐。下面就介紹一下一些核心對(duì)象。
三.ImageProcessor核心對(duì)象解析
? ? 解析來(lái)我們具體了解一下核心的方法和屬性,看看源碼還是有好處。
? 1.ImageFactory.Load()
public ImageFactory Load(string imagePath)
? ? ? ? {
? ? ? ? ? ? FileInfo fileInfo = new FileInfo(imagePath);
? ? ? ? ? ? if (fileInfo.Exists)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? this.ImagePath = imagePath;
? ? ? ? ? ? ? ? using (FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ISupportedImageFormat format = FormatUtilities.GetFormat(fileStream);
? ? ? ? ? ? ? ? ? ? if (format == null)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? throw new ImageFormatException("Input stream is not a supported format.");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? MemoryStream memoryStream = new MemoryStream();
? ? ? ? ? ? ? ? ? ? fileStream.CopyTo(memoryStream);
? ? ? ? ? ? ? ? ? ? memoryStream.Position = 0;
? ? ? ? ? ? ? ? ? ? this.Image = format.Load(memoryStream);
? ? ? ? ? ? ? ? ? ? this.CurrentBitDepth = Image.GetPixelFormatSize(this.Image.PixelFormat);
? ? ? ? ? ? ? ? ? ? this.InputStream = memoryStream;
? ? ? ? ? ? ? ? ? ? format.Quality = DefaultQuality;
? ? ? ? ? ? ? ? ? ? format.IsIndexed = FormatUtilities.IsIndexed(this.Image);
? ? ? ? ? ? ? ? ? ? this.backupFormat = format;
? ? ? ? ? ? ? ? ? ? this.CurrentImageFormat = format;
? ? ? ? ? ? ? ? ? ? foreach (PropertyItem propertyItem in this.Image.PropertyItems)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? this.ExifPropertyItems[propertyItem.Id] = propertyItem;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? this.backupExifPropertyItems = this.ExifPropertyItems;
? ? ? ? ? ? ? ? ? ? IAnimatedImageFormat imageFormat = this.CurrentImageFormat as IAnimatedImageFormat;
? ? ? ? ? ? ? ? ? ? if (imageFormat != null)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? imageFormat.AnimationProcessMode = this.AnimationProcessMode;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? Image formatted = this.Image.Copy(this.AnimationProcessMode);
? ? ? ? ? ? ? ? ? ? this.Image.Dispose();
? ? ? ? ? ? ? ? ? ? this.Image = formatted;
? ? ? ? ? ? ? ? ? ? this.ShouldProcess = true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? throw new FileNotFoundException(imagePath);
? ? ? ? ? ? }
? ? ? ? ? ? return this;
? ? ? ? }
該方法用來(lái)加載要處理的圖像的方法, 始終首先調(diào)用此方法。該方法具有4個(gè)重載版本,分別接收的參數(shù)為string,byte[],Image,Stream。FormatUtilities.GetFormat(fileStream)方法從給定流獲取正確的ISupportedImageFormat。在對(duì)圖片的數(shù)據(jù)流進(jìn)行操作時(shí),首先會(huì)復(fù)制圖片的流數(shù)據(jù)。format.Load(memoryStream)將我們的映像設(shè)置為內(nèi)存流值。圖片數(shù)據(jù)流進(jìn)行一個(gè)操作后,會(huì)調(diào)用Image.Copy(this.AnimationProcessMode)確保圖像是最有效的格式。
? ?2.ImageFactoryExtensions.AutoProcess()?
internal static ImageFactory AutoProcess(this ImageFactory factory, IWebGraphicsProcessor[] graphicsProcessors)
? ? ? ? {
? ? ? ? ? ? if (factory.ShouldProcess)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? foreach (IWebGraphicsProcessor graphicsProcessor in graphicsProcessors)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? factory.CurrentImageFormat.ApplyProcessor(graphicsProcessor.Processor.ProcessImage, factory);
? ? ? ? ? ? ? ? ? ? IDisposable disposable = graphicsProcessor.Processor.DynamicParameter as IDisposable;
? ? ? ? ? ? ? ? ? ? disposable?.Dispose();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return factory;
? ? ? ? }
? ? ?ImageFactoryExtensions類(lèi)是ImageFactory類(lèi)的擴(kuò)展類(lèi),主要是擴(kuò)展Web項(xiàng)目。AutoProcess()方法基于添加到圖像路徑的任何查詢(xún)字符串參數(shù),自動(dòng)處理圖像文件。graphicsProcessors參數(shù)表示要應(yīng)用的圖形處理器陣列。graphicsProcessor.Processor.DynamicParameter as IDisposable打開(kāi)動(dòng)態(tài)參數(shù)并處理任何需要它的類(lèi)型。
? ?3.ImageProcessingModule.SetHeaders()
public static void SetHeaders(HttpContext context, int maxDays)
? ? ? ? {
? ? ? ? ? ? object responseTypeObject = context.Items[CachedResponseTypeKey];
? ? ? ? ? ? object dependencyFileObject = context.Items[CachedResponseFileDependency];
? ? ? ? ? ? string responseType = responseTypeObject as string;
? ? ? ? ? ? string[] dependencyFiles = dependencyFileObject as string[];
? ? ? ? ? ? SetHeaders(context, responseType, dependencyFiles, maxDays);
? ? ? ? }
在Web擴(kuò)展中,ImageProcessingModule類(lèi)比較重要,處理Web應(yīng)用程序中的任何圖像請(qǐng)求。SetHeaders()方法使瀏覽器和服務(wù)器將輸出保存在其緩存中,從而提高性能。該方法接受兩個(gè)參數(shù),context表示請(qǐng)求的http消息對(duì)象,HttpContext對(duì)象對(duì)內(nèi)在服務(wù)器對(duì)象的引用。maxDays參數(shù)表示將圖片存儲(chǔ)在瀏覽器緩存中的最長(zhǎng)天數(shù)。
四.總結(jié)
? ?說(shuō)句實(shí)話,這位作者的編碼風(fēng)格是喜歡的,代碼簡(jiǎn)介明了,沒(méi)有那么多裝逼的寫(xiě)法,不會(huì)為了使用一些寫(xiě)法,而去改變代碼的可讀性。對(duì)于這個(gè)組件系列,我會(huì)近可能的寫(xiě)一些,大家可以借此了解一些組件,需要深入了解和使用的,可以自己查看源碼,進(jìn)行對(duì)應(yīng)的擴(kuò)展。寫(xiě)完這篇,已經(jīng)凌晨?jī)牲c(diǎn)了,為自己點(diǎn)個(gè)贊,無(wú)論寫(xiě)的怎樣,覺(jué)得自己還是盡心了。
原文地址:http://www.cnblogs.com/pengze0902/p/6569360.html
.NET社區(qū)新聞,深度好文,微信中搜索dotNET跨平臺(tái)或掃描二維碼關(guān)注
總結(jié)
以上是生活随笔為你收集整理的开源免费的.NET图像即时处理的组件ImageProcessor的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: .NET Core项目从xproj+pr
- 下一篇: asp.net ajax控件工具集 Au