0

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
Adam
  • 6,041
  • 36
  • 120
  • 208
  • 1
    You never call `img.UnlockBits(bmpData)` to free that memory. So that may be the source of the error. – TnTinMn May 18 '18 at 14:39
  • @TnTinMn: I tried that, but in that case no image is saved anymore and I get the error Parameter is not valid. when trying to save the file. I updated my post to show my full code..if you could have a look? – Adam May 18 '18 at 17:57
  • 1
    Move the UnlockBits call to be before you dispose the image. If that does not resolve the issue, you are the one in the position to debug the code and determine which _parameter_ is invalid and why it is invalid. Set a breakpoint and step through the code. – TnTinMn May 18 '18 at 18:52

0 Answers0