Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 651
Adam Avatar asked Apr 28 '18 15:04

Adam


2 Answers

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

After the conversion process, you need to marshall unmanaged memory so you can use the data in managed code.

Public Sub Encode()
    Dim source As Bitmap = New Bitmap(Server.MapPath("images\audio.png"))
    'Hold bitmap data
    Dim bmpData As BitmapData = source.LockBits(New Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb)

    'Create a pointer for webp data
    Dim webpDataSrc As IntPtr

    'Store resulting webp data length after conversion
    Dim webpDataLen As Integer = WebPEncodeBGRA(bmpData.Scan0, source.Width, source.Height, bmpData.Stride, 80, webpDataSrc)

    'Create a managed byte array with the size you just have
    Dim webpDataBin As Byte() = New Byte(webpDataLen - 1){}

    'Copy from unmanaged memory to managed byte array you created
    System.Runtime.InteropServices.Marshal.Copy(webpDataSrc, webpDataBin, 0, webpDataLen)

    'Write byte array to a file
    System.IO.File.WriteAllBytes(Server.MapPath("images\audio.webp"), webpDataBin)

    'Free
    WebPFree(webpDataSrc)

    source.Dispose()
End Sub

BTW The command nmake /f Makefile.vc CFG=release-dynamic RTLIBCFG=dynamic OBJDIR=output ARCH=x64 you say you used is OK. I also could compile the library (version 1.0.0) with that command too. However I'm 100% positive that zipped libwebp.dll here is a 32-bit one, not 64-bit.

Make sure that you start the right environment to produce 64-bit binaries.

like image 86
Kul-Tigin Avatar answered Nov 11 '22 11:11

Kul-Tigin


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")
like image 4
Norbert Ziemniak Avatar answered Nov 11 '22 09:11

Norbert Ziemniak