article

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

Email
Submitted on: 3/24/2016 9:31:35 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 1936
 
     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. Here are some important steps for the conversion, 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 and Save WebP image.

 
				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[i] 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.


Other 44 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.