VB.NET: How to save a WebP encoded image to disk

706 views
Skip to first unread message

flora...@gmail.com

unread,
May 1, 2018, 2:59:18 PM5/1/18
to WebP Discussion
I have the following code:
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("input.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

But how can I get the encoded WebP image and save that to disk?

James Zern

unread,
May 1, 2018, 7:55:32 PM5/1/18
to WebP Discussion, flora...@gmail.com
Hi,
The webp image is stored to webp_data, its size is the return value of WebPEncodeBGRA. In another C# example Marshal.Copy was used to move the unmanaged data (webp_data) to something that the language could work with to store a file.

James Zern

unread,
May 9, 2018, 3:56:43 PM5/9/18
to flora...@gmail.com, WebP Discussion
+webp-discuss

On Wed, May 9, 2018 at 12:56 PM James Zern <jz...@google.com> wrote:


On Wed, May 2, 2018 at 1:35 AM Floran Felen <flora...@gmail.com> wrote:
Hi,

Thank you for this. The problem is actually a bit broader, I created an SO post with a 150rep bounty: https://stackoverflow.com/questions/50078087/asp-net-libwebp-dll-how-to-save-webp-image-to-disk, maybe you can see what's going on? Others on SO apparently don't.
Also, do you have a link to this other C# that you mention?

Sorry I didn't include the link before. I think the origin of my comment was this thread [1] and the source [2].
 

Thank you in advance!

 

Cheers,

Floran 

Reply all
Reply to author
Forward
0 new messages