翻譯|使用教程|編輯:李顯亮|2020-07-06 13:51:19.963|閱讀 907 次
概述:在本文中,我將向您展示如何從圖像或多頁(yè)Tiff 創(chuàng)建動(dòng)畫(huà)PNG(APNG)圖像,以及如何使用C#(或VB.NET)以編程方式將APNG導(dǎo)出到動(dòng)畫(huà)GIF。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
動(dòng)畫(huà)PNG(便攜式網(wǎng)絡(luò)圖形)是對(duì)PNG格式的擴(kuò)展,以合并動(dòng)畫(huà)。就像動(dòng)畫(huà)GIF一樣,APNG格式用于呈現(xiàn)動(dòng)畫(huà)。APNG相對(duì)于GIF的優(yōu)勢(shì)在于,它支持24位透明性,而GIF僅支持8位。此外,與動(dòng)畫(huà)GIF相比,APNG提供了更流暢的動(dòng)畫(huà)。
Aspose提供其.NET Imaging API,以使用C#或VB.NET創(chuàng)建或處理流行的圖像格式,包括動(dòng)畫(huà)PNG。接下來(lái)將利用Aspose.Imaging for .NET的功能來(lái)創(chuàng)建或?qū)С鰟?dòng)畫(huà)PNG圖像。
在本文中,我將向您展示如何從圖像或多頁(yè)Tiff 創(chuàng)建動(dòng)畫(huà)PNG(APNG)圖像,以及如何使用C#(或VB.NET)以編程方式將APNG導(dǎo)出到動(dòng)畫(huà)GIF。
目前發(fā)布了Aspose.Imaging for .NET v20.6,支持APNG(動(dòng)畫(huà)PNG)文件格式,支持BMP的新壓縮方法DXT1 ,支持批量導(dǎo)出到WebP以獲得多頁(yè)圖像,還沒(méi)使用過(guò)的朋友可以點(diǎn)擊下載最新版Aspose.Imaging
Aspose.Imaging for .NET允許通過(guò)設(shè)置自定義動(dòng)畫(huà)和幀持續(xù)時(shí)間,從單頁(yè)圖像(例如PNG)創(chuàng)建動(dòng)畫(huà)PNG。以下是執(zhí)行此操作的步驟。
下面的代碼示例演示如何使用C#從PNG圖像創(chuàng)建動(dòng)畫(huà)PNG(APNG)。
using Aspose.Imaging; using Aspose.Imaging.ImageOptions; using Aspose.Imaging.FileFormats.Apng; const int AnimationDuration = 1000; // 1 s const int FrameDuration = 70; // 70 ms using (RasterImage sourceImage = (RasterImage)Image.Load("not_animated.png")) { ApngOptions createOptions = new ApngOptions { Source = new FileCreateSource("raster_animation.png", false), DefaultFrameTime = (uint)FrameDuration, ColorType = PngColorType.TruecolorWithAlpha, }; using (ApngImage apngImage = (ApngImage)Image.Create( createOptions, sourceImage.Width, sourceImage.Height)) { int numOfFrames = AnimationDuration / FrameDuration; int numOfFrames2 = numOfFrames / 2; apngImage.RemoveAllFrames(); // add first frame apngImage.AddFrame(sourceImage, FrameDuration); // add intermediate frames for (int frameIndex = 1; frameIndex < numOfFrames - 1; ++frameIndex) { apngImage.AddFrame(sourceImage, FrameDuration); ApngFrame lastFrame = (ApngFrame)apngImage.Pages[apngImage.PageCount - 1]; float gamma = frameIndex >= numOfFrames2 ? numOfFrames - frameIndex - 1 : frameIndex; lastFrame.AdjustGamma(gamma); } // add last frame apngImage.AddFrame(sourceImage, FrameDuration); apngImage.Save(); } }
使用多頁(yè)的Tiff文件通過(guò)設(shè)置所需的幀時(shí)間來(lái)創(chuàng)建動(dòng)畫(huà)PNG。以下是從Tiff文件創(chuàng)建動(dòng)畫(huà)PNG的步驟。
下面的代碼示例演示如何使用C#從Tiff文件創(chuàng)建動(dòng)畫(huà)PNG。
using Aspose.Imaging; using Aspose.Imaging.ImageOptions; using (Image image = Image.Load("img4.tif")) { // Setting up the default frame duration image.Save("img4.tif.500ms.png", new ApngOptions() { DefaultFrameTime = 500 }); // 500 ms image.Save("img4.tif.250ms.png", new ApngOptions() { DefaultFrameTime = 250 }); // 250 ms }
將動(dòng)畫(huà)PNG圖像導(dǎo)出到等效的動(dòng)畫(huà)GIF。以下是將APNG導(dǎo)出到GIF的步驟。
下面的代碼示例演示如何使用C#將動(dòng)畫(huà)PNG導(dǎo)出到GIF。
using System.Diagnostics; using Aspose.Imaging; using Aspose.Imaging.ImageOptions; using Aspose.Imaging.FileFormats.Apng; using (Image image = Image.Load("elephant.png")) { // Checking the type of loaded image Debug.Assert(image is ApngImage); // Save to the same format image.Save("elephant_same_format.png"); // Export to the other animated format image.Save("elephant.png.gif", new GifOptions()); }
輸出結(jié)果
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn