Home > Code > C# > How to Convert GIFF Image Frame to WebP Image inside .NET applications

How to Convert GIFF Image Frame to WebP Image inside .NET applications

by sher azam   on Mar 24, 2016   Category: C#   |  Views: 480    |  Points: 25   |  Gold 


This technical tip explains how .NET developers can Convert GIFF Image Frame to WebP Image inside their .NET applications. Aspose.Imaging enables you to extract a frame or frames from any existing GIFF image and convert it to WebP format. This article shows how you can extract a particular frame from a GIFF image and convert it to a WebP image. Following are the steps to convert a GIFF frame to a WebP image.

• Load an existing GIFF image into an instance of Image using the factory method Load.
• Create and initialize an instance of GifImage class.
• Create and initialize an instance of WebPImage class.
• Access the GIFF image Frame with GifImage.Blocks property.
• Create and initialize an instance of WebPFrameBlock class.
• Add WebP frame to WebP image block list.
• Set WebP image options.
• Save WebP image.

// Converting GIFF Image Frame to WebP Image
//[C# Code Sample]

// Load GIFF image into the instance of image class.
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(@"D:\animation.gif"))
{
// Create an instance of GIFF image class.
Aspose.Imaging.FileFormats.Gif.GifImage gif = image as Aspose.Imaging.FileFormats.Gif.GifImage;

if (gif == null) return;

// Load an existing WebP image into the instance of WebPImage class.
using (Aspose.Imaging.FileFormats.Webp.WebPImage webp = new Aspose.Imaging.FileFormats.Webp.WebPImage(image.Width, image.Height, null))
{
// Loop through the GIFF frames
for (int i = 0; i < gif.Blocks.Length; i++)
{
// Convert GIFF block to GIFF Frame
Aspose.Imaging.FileFormats.Gif.Blocks.GifFrameBlock gifBlock = gif.Blocks as Aspose.Imaging.FileFormats.Gif.Blocks.GifFrameBlock;
if (gifBlock == null)
{
continue;
}

// Create an instance of WebP Frame instance by passing GIFF frame to class constructor.
Aspose.Imaging.FileFormats.Webp.WebPFrameBlock block = new Aspose.Imaging.FileFormats.Webp.WebPFrameBlock(gifBlock)
{
Top = (short)gifBlock.Top,
Left = (short)gifBlock.Left,
Duration = (short)gifBlock.ControlBlock.DelayTime
};

// Add WebP frame to WebP image block list
webp.AddBlock(block);
}

// Set Properties of WebP image.
webp.Options.AnimBackgroundColor = 0xff; //black
webp.Options.AnimLoopCount = 0; //infinity
webp.Options.Quality = 50;
webp.Options.Lossless = false;

// Save WebP image.
webp.Save(@"D:\saveAnimation.webp");
}
}

//[VB.NET Code Sample]

' Load GIFF image into the instance of image class.
Using image As Aspose.Imaging.Image = Aspose.Imaging.Image.Load("D:\animation.gif")

' Create an instance of GIFF image class.
Dim gif As Aspose.Imaging.FileFormats.Gif.GifImage = TryCast(image, Aspose.Imaging.FileFormats.Gif.GifImage)

If gif Is Nothing Then
Return
End If

' Load an existing WebP image into the instance of WebPImage class.
Using webp As New Aspose.Imaging.FileFormats.Webp.WebPImage(image.Width, image.Height, Nothing)
' Loop through the GIFF frames
For i As Integer = 0 To gif.Blocks.Length - 1
' Convert GIFF block to GIFF Frame
Dim gifBlock As Aspose.Imaging.FileFormats.Gif.Blocks.GifFrameBlock = TryCast(gif.Blocks(i), Aspose.Imaging.FileFormats.Gif.Blocks.GifFrameBlock)
If gifBlock Is Nothing Then
Continue For
End If

' Create an instance of WebP Frame instance by passing GIFF frame to class constructor.
Dim block As New Aspose.Imaging.FileFormats.Webp.WebPFrameBlock(gifBlock) With { _
Key .Top = CShort(gifBlock.Top), _
Key .Left = CShort(gifBlock.Left), _
Key .Duration = CShort(gifBlock.ControlBlock.DelayTime) _
}

' Add WebP frame to WebP image block list
webp.AddBlock(block)
Next

' Set Properties of WebP image.
webp.Options.AnimBackgroundColor = &Hff
'black
webp.Options.AnimLoopCount = 0
'infinity
webp.Options.Quality = 50
webp.Options.Lossless = False

' Save WebP image.
webp.Save("D:\saveAnimation.webp")
End Using
End Using

Overview: Aspose.Imaging for .NET

Aspose.Imaging for .NET is an image processing & manipulation component that allows developers to create, edit, draw or convert images in their .NET application. It allows developers to convert image files to PSD, BMP, JPEG, PNG, TIFF and GIF formats. Moreover a set of pens, brushes and fonts can be used to draw images or add new elements & text to existing images. Aspose.Imaging for .NET works well with both web & windows applications. Moreover, it adds the support for Silverlight platform.

More about Aspose.Imaging for .NET

- Homepage of Aspose.Imaging for .NET: http://www.aspose.com/.net/imaging-component.aspx

- Download Aspose.Imaging for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.imaging-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts