Category:图像图像
Article From:https://www.cnblogs.com/lonelyxmas/p/9122478.html
Using LibWebP-NET to decode and encode WebP format pictures.

WebP Format is a newly developed network picture format developed by Google. It has the advantages of high compression rate, fast decoding, no copyright and open source.

Most advanced web browsers and photo browsers support this format, and Windows users can look up WebP images in the system by installing WebP Codec for Windows.

Although WebP is an open source format, at present,.NET platform is not good support for WebP, and can only rely on the third party class library to load WebP format.

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

LibWebP-NET It is simply a.NET package for the C++ class library of WebP, whose core code is still in the libwebp.dll, a dynamic link library file, so it does not support running in the ordinary ASP.NET space.

And because the dynamic link library limits the system, there are two versions of C++ class library files, x86 and x64, which are applied to different operating systems.

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

 

The following test code for my modified version of the LibWebPNET source class library files, you can click here to download.

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

Before you use, remember to quote 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("The specified WebP picture")
Dim img As Bitmap = Imazen.WebP.SimpleDecoder.DecodeFromBytes(bs, bs.Length)

In this way, WebP images can be decoded as.NET recognizable picture objects and applied to other kinds of operations.

 

The encoding method is slightly complex

Dim pic As Image = Image.FromFile("Original picture position")

' WebPOnly two pixel formats, Format32bppArgb and Format32bppRgb, are supported.
' So sometimes you need to change the code and redraw an image
Using bmp As New Bitmap(pic.Width, pic.Height, Imaging.PixelFormat.Format32bppArgb)
    ' Redraw the picture to the new canvas
    Using g As Graphics = Graphics.FromImage(bmp)
        g.DrawImage(pic, 0, 0, pic.Width, pic.Height)
    End Using
    ' Transcoding and saving the file
    Using fs As IO.FileStream = IO.File.Create("New picture location.Webp")
        Imazen.WebP.SimpleEncoder.Encode(bmp, fs, 100)
    End Using

End Using

WebP Coding only supports Format32bppArgb and Format32bppRgb, and the current mainstream network picture format is not the two pixel format, so we need to transcode the picture before we transform it.

 

With LibWebP-NET, we can easily embrace WebP’s future network image format. It is hoped that.NET will also have built-in support for WebP instead of the third party class library.

Leave a Reply

Your email address will not be published. Required fields are marked *