ASP.NET libwebp.dll如何将WebP映像保存到磁盘

时间:2018-04-28 15:24:18

标签: asp.net windows vb.net image webp

我使用these instructions(我已下载此source code)为WebP构建了libwebp.dll

我已将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

我收到错误:

  

尝试加载格式不正确的程序。 (HRESULT异常:0x8007000B)

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

  • 我为64位架构构建了这样的dll:nmake /f Makefile.vc CFG=release-dynamic RTLIBCFG=dynamic OBJDIR=output ARCH=x64also see here
  • 我检查了IIS中有问题的应用程序池,并将属性Enable32-Bit Applications设置为False
  • 我正在运行Windows 10 64位
  • 代码隐藏中的Environment.Is64BitProcessEnvironment.Is64BitOperatingSystem评估为True

如何编码图像并将编码图像以WebP格式保存到磁盘?

在您的计算机上进行测试
下载已编译的zipped libwebp.dll here。这是我正在使用的图像文件:

enter image description here

2 个答案:

答案 0 :(得分:5)

  

如何编码图像并将编码图像以WebP格式保存到磁盘?

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

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位二进制文​​件。

答案 1 :(得分:4)

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

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

    <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.0.zip的dll,它可以正常工作。

当我尝试附上你的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")