I'm getting the error There is insufficient system memory in resource pool 'internal' to run this query. I've alread checked out this post: There is insufficient system memory in resource pool 'default' to run this query. on sql
However, the error occurs when I added the following code (also see ASP.NET libwebp.dll how to save WebP image to disk):
As you can see below WebPFree doesn't clear the memory reserved by the variable of type IntPtr. I'm not sure what code should be added in WebPFree and how to clear the used memory.
I also checked this post, but found no solution either.
Dim imgRequest As WebRequest = WebRequest.Create(imageURL)
Dim imgResponse As WebResponse
Dim memStream As New MemoryStream
imgResponse = imgRequest.GetResponse()
Dim streamPhoto As Stream = imgResponse.GetResponseStream()
streamPhoto.CopyTo(memStream)
memStream.Position = 0
Dim bfPhoto As BitmapFrame = ReadBitmapFrame(memStream)
Dim baResize As Byte() = ToByteArray(bfPhoto)
Dim bmp As System.Drawing.Bitmap = imageFunctions.ConvertByteArrayToBitmap(baResize)
File.WriteAllBytes(test.webp, imageFunctions.EncodeImageToWebP(bmp))
Public Class imageFunctions
<DllImport("libwebp.dll", CallingConvention:=CallingConvention.Cdecl)>
Public 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)>
Public Shared Function WebPFree(ByVal p As IntPtr) As Integer
End Function
Public Shared Function EncodeImageToWebP(ByVal img As System.Drawing.Bitmap) As Byte()
Dim bmpData As BitmapData = img.LockBits(New Drawing.Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.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, img.Width, img.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)
'Free
WebPFree(webpDataSrc)
img.Dispose()
img.UnlockBits(bmpData)
Return webpDataBin
End Function
Public Shared Function ConvertByteArrayToBitmap(ByVal imageData As Byte()) As System.Drawing.Bitmap
Dim ms As New IO.MemoryStream(imageData)
Return New Drawing.Bitmap(ms)
End Function
End Class