Uses LibWebP-NET to decode and encode WebP images, libwebp-netwebp

Source: Internet
Author: User
Tags webp

Uses LibWebP-NET to decode and encode WebP images, libwebp-netwebp

WebP is the latest network image format developed and developed by Google. It has the advantages of high compression rate, fast decoding, copyright-free, and open source.

Most advanced Web browsers and image browsers support this format. for Windows users, you can view WebP images by installing WebP Codec for Windows.

Although WebP is an open-source format, currently. NET platforms do not support WebP well. We can only rely on third-party class libraries to load the WebP format.

Here I recommend LibWebP-NET, an open-source class library, which can decode and decode WebP images in. NET.

LibWebP-NET is just a simple C ++ class library for WebP. the core code is still in libwebp. dll is a dynamic link library file, so it is not supported in common ASP.. NET.

Because of the system limitations imposed by the dynamic link library, C ++ class library files of x86 and x64 versions are available for different operating systems.

I personally recommend that you use the x86 compatibility mode in the compilation options of. NET project properties, so that you only need to attach the x86 version of libwebp. dll file.

 

The following test code is a modified version of the LibWebPNET source code library file. You can click here to download it.

I only set the fast encoding and decoding functions as static methods (the original version needs to instantiate the related decoding classes, which is very cumbersome ).

Before use, remember to reference the Imazen. WebP. dll class library and release the libwebp. dll Dynamic Link Library to the running directory of the main program.

 

The decoding method is very simple.

Dim bs As Byte () = IO. File. ReadAllBytes ("specified WebP image") Dim img As Bitmap = Imazen. WebP. SimpleDecoder. DecodeFromBytes (bs, bs. Length)

In this way, the WebP image can be decoded as A. NET identifiable image object and applied to other operations.

 

The encoding method is slightly complicated.

Dim pic As Image = Image. fromFile ("Original Image Location") 'webp only supports Format32bppArgb and Format32bppRgb two pixel format'. Therefore, you may need to re-paint an image Using bmp As New Bitmap (pic. width, pic. height, Imaging. pixelFormat. format32bppArgb) 'repaint the image to the new canvas Using g As Graphics = Graphics. fromImage (bmp) g. drawImage (pic, 0, 0, pic. width, pic. height) End Using 'transcode and save the file Using fs As IO. fileStream = IO. file. create ("New Image Location. webp ") Imazen. webP. simpleEncoder. encode (bmp, fs, 100) End UsingEnd Using

WebP encoding only supports Format32bppArgb and Format32bppRgb. Currently, the mainstream network image formats are not the two pixel formats. Therefore, before conversion, We Need To transcode the image.

 

Through LibWebP-NET, we can easily embrace the future network image format of WebP. We hope that. NET will also support WebP in the future, instead of third-party class libraries.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.