Search code examples
asp.netwindowsvb.netimagewebp

ASP.NET libwebp.dll how to save WebP image to disk


I've built libwebp.dll for WebP, using these instructions (I downloaded this source code)

I've added the libwebp.dll file to the bin folder of my project.

I then added this code (found here):

Private Declare Function WebPEncodeBGRA Lib "libwebp.dll" (ByVal rgba As IntPtr, ByVal width As Integer, ByVal height As Integer, ByVal stride As Integer, ByVal quality_factor As Single, ByRef output As IntPtr) As Integer
Private Declare Function WebPFree Lib "libwebp.dll" (ByVal p As IntPtr) As Integer

Private Sub Encode()
    Dim source As Bitmap = New Bitmap(Server.MapPath("images\") + "audio.png")
    Dim data As BitmapData = source.LockBits(New Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
    Dim webp_data As IntPtr
    Dim i As Integer = WebPEncodeBGRA(data.Scan0, source.Width, source.Height, data.Stride, 80, webp_data)

    WebPFree(webp_data)
End Sub

I get the error:

An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

What I also did (after comments from Dai below):

  • I built the dll for a 64-bit architecture like so: nmake /f Makefile.vc CFG=release-dynamic RTLIBCFG=dynamic OBJDIR=output ARCH=x64 (also see here)
  • I checked IIS for the application pool in question and it has the property Enable32-Bit Applications set to False
  • I'm running Windows 10 64 bit
  • Both Environment.Is64BitProcess and Environment.Is64BitOperatingSystem in code-behind evaluate as True

How can I encode the image and save the encoded image to disk in WebP format?

This is the image file I'm using:

enter image description here


Solution

  • i also have problem with yours sample, so i check sources and looks like u use different approach then is in sample, don't know why.

    so i change and looks it works.

        <DllImport("libwebp.dll", CallingConvention:=CallingConvention.Cdecl)>
    Private Shared Function WebPEncodeBGRA(ByVal rgba As IntPtr, ByVal width As Integer, ByVal height As Integer, ByVal stride As Integer, ByVal quality_factor As Single, <Out> ByRef output As IntPtr) As Integer
    End Function
    
    
    <DllImport("libwebp.dll", CallingConvention:=CallingConvention.Cdecl)>
    Private Shared Function WebPFree(ByVal p As IntPtr) As Integer
    End Function
    

    and import

    Imports System.Runtime.InteropServices
    

    i Have use dll from Download libwebp-0.6.0.zip from Thise instructions 'libwebp-0.6.0.zip\x64\bin' and it works.

    When i try attache your dll from Download the compiled and zipped libwebp.dll here. This is the image file I'm using:

    got exactly this same exception, looks like is not 64

    for image i'm not sure, probaly u need reverse it

                'data.strinde = webp_data 'not sure what is webp_data ewxacly, but you can check
            'data.Scan0 = webp_data 'not sure what is webp_data ewxacly, but you can check
            source.UnlockBits(data)
            source.Save(Server.MapPath("images\") + "audio.png")