Asp.net

ASP.NET libwebp.dll 如何將 WebP 圖像保存到磁碟

  • January 10, 2022

我使用這些說明libwebp.dll為 WebP建構 (我下載了這個原始碼

我已將該文件添加libwebp.dllbin我的項目的文件夾中。

然後我添加了這段程式碼(在這裡找到):

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)

我也做了什麼(在下面 Dai 的評論之後):

  • 我為 64 位架構建構了 dll,如下所示nmake /f Makefile.vc CFG=release-dynamic RTLIBCFG=dynamic OBJDIR=output ARCH=x64:(另見此處
  • 我檢查了有問題的應用程序池的 IIS,它的屬性Enable32-Bit Applications設置為False
  • 我正在執行 Windows 10 64 位
  • Environment.Is64BitProcessEnvironment.Is64BitOperatingSystem在程式碼隱藏中評估為True

如何對圖像進行編碼並將編碼的圖像以 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 指令“libwebp-0.6.0.zip\x64\bin”下載 libwebp-0.6.0.zip 的 dll,它可以工作。

當我嘗試從此處下載已編譯和壓縮的 libwebp.dll 中附加您的 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")

如何對圖像進行編碼並將編碼的圖像以 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

順便說一句,nmake /f Makefile.vc CFG=release-dynamic RTLIBCFG=dynamic OBJDIR=output ARCH=x64您說您使用的命令是可以的。我也可以使用該命令編譯庫(版本 1.0.0)。但是我 100% 肯定這裡壓縮的 libwebp.dll是 32 位的,而不是 64 位的。

確保您啟動了正確的環境來生成 64 位二進製文件。

引用自:https://stackoverflow.com/questions/50078087