gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:21:34 27 4
gpt4 key购买 nike

我已经为 WebP 构建了 libwebp.dll,使用 these instructions (我下载了这个 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)

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

  • 我为 64 位架构构建了 dll,如下所示:nmake/f Makefile.vc CFG=release-dynamic RTLIBCFG=dynamic OBJDIR=output ARCH=x64 ( also see here )<
  • 我检查了 IIS 中有问题的应用程序池,它的属性 Enable32-Bit Applications 设置为 False
  • 我运行的是 Windows 10 64 位
  • 代码隐藏中的 Environment.Is64BitProcessEnvironment.Is64BitOperatingSystem 都评估为 True

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

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

enter image description here

最佳答案

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

关于ASP.NET libwebp.dll 如何将 WebP 图像保存到磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50078087/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com