web analytics

How to get the width and height of a WebP format image in C#?

Options

codeling 1599 - 6654
@2019-12-30 16:32:51

As you know, WebP is an Open Source image format developed at Google, which promises to generate images smaller in size compared to JPG and PNG formats, while generating better looking images.

WebP supports transparency, like PNG and GIF images, also WebP supports animations, like GIF images. So WebP can do all GIF, JPG and PNG images can do, in a single format, and generate smaller images. 

Currently, WebP images can be displayed in all major browsers such as Chrome, IE, Edge, Firefox, Safari, etc.

If you are developing a Web application to display a WebP image in the browser, and you want to know its width and height to resize the WebP image to fit the screen, the following code demonstrates how to get the width and height of a WebP format image in C#.

string imageUrl = "https://www.gstatic.com/webp/gallery/1.sm.webp"

using (WebClient c = new WebClient())
{
    c.Headers.Add("User-Agent: Other");
    byte[] responseData = c.DownloadData(imageFileUrl);

    if (responseData.Length > 0)
    {
        int imgWidth, imgHeight;

        using (MemoryStream ms = new MemoryStream(responseData))
        {

            bool isWebP = false;

            var webPCheck = new byte[4];

            if (responseData.Length > 4)
            {
                webPCheck[0] = responseData[0];
                webPCheck[1] = responseData[1];
                webPCheck[2] = responseData[2];
                webPCheck[3] = responseData[3];

                var firstStr = Encoding.ASCII.GetString(webPCheck, 0, 4);

                if (firstStr.ToUpper() == "RIFF")
                {
                    isWebP = true;
                }
            }

            if (isWebP)
            {
                Imazen.WebP.Extern.LoadLibrary.LoadWebPOrFail();
                var decoder = new Imazen.WebP.SimpleDecoder();
                using (Bitmap bmp = decoder.DecodeFromBytes(responseData, responseData.LongLength))
                {
                    imgWidth = bmp.Width;
                    imgHeight = bmp.Height;
                }
            }
        }
    }
}

But before you can run the C# code, you have to do the following tasks first:

1.Install Imazen.WebP package through NuGet Manager in the Visual Studio.

2.Since the Imanzen.WebP is a .NET wrapper to Google's WebP encode and decode library libwebp.dll, you have to download this library or compile the source code from Github to create the library by yourself, and save these libraries to the following highlighted folders in your project:

After that, you can compile and run your code.

 

@2019-12-30 17:02:38

You can't call Image.FromStream or Image.FromFile to load a WebP format image directly, the following code will throw an exeception:

string imageUrl = "https://www.gstatic.com/webp/gallery/1.sm.webp"

using (WebClient c = new WebClient())
{
    c.Headers.Add("User-Agent: Other");
    byte[] responseData = c.DownloadData(imageFileUrl);

    if (responseData.Length > 0)
    {
        int imgWidth, imgHeight;

        using (MemoryStream ms = new MemoryStream(responseData))
        {
            using (Image img = Image.FromStream(ms))
            {
                imgWidth = bmp.Width;
                imgHeight = bmp.Height;
            }
        }
    }
}

 

@2019-12-30 17:15:03

If you want some WebP image samples, here’s a WebP gallery by Google.

@2019-12-30 20:59:03

Online image converter 

Convert your file from Google Web Picture files to Portable Network Graphics with this WEBP to PNG converter.

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com