ASP.NET libwebp.dll 如何将 WebP 图片保存到磁盘

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

我已经为 WebP 构建了 libwebp.dll,使用 these instructions (I downloaded this source code)

我已将 libwebp.dll 文件添加到我项目的 bin 文件夹中。

然后我添加了这段代码 (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

我收到错误:

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

我也做了什么(在下面戴的评论之后):

如何对图像进行编码并将编码后的图像以WebP格式保存到磁盘?

这是我正在使用的图像文件:

我对你的样本也有问题,所以我检查了来源,看起来你使用了不同的方法,然后在样本中,不知道为什么。

所以我改变了,看起来很有效。

    <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

并导入

Imports System.Runtime.InteropServices

我使用了从 Thise instructions 'libwebp-0.6.0.zip\x64\bin' 下载 libwebp-0.6 的 dll。0.zip 并且它有效。

当我尝试附加你的 dll 时 在此处下载已编译和压缩的 libwebp.dll。这是我正在使用的图像文件:

得到完全相同的异常,看起来不是 64

对于图像我不确定,可能你需要反转它

            '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")

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

转换过程完成后,您需要编组非托管内存,以​​便可以在托管代码中使用数据。

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 你说的命令nmake /f Makefile.vc CFG=release-dynamic RTLIBCFG=dynamic OBJDIR=output ARCH=x64是可以的。我也可以使用该命令编译库(版本 1.0.0)。但是,我 100% 肯定 zipped libwebp.dll here 是 32 位的,而不是 64 位的。

确保启动正确的环境以生成 64 位二进制文​​件。