Очистить целевой объект IntPtr в VB.NET - PullRequest
0 голосов
/ 18 мая 2018

Я получаю сообщение об ошибке There is insufficient system memory in resource pool 'internal' to run this query. Я уже проверил этот пост: Недостаточно системной памяти в пуле ресурсов 'default' для выполнения этого запроса.на sql

Однако ошибка возникает, когда я добавил следующий код (см. также ASP.NET libwebp.dll, как сохранить образ WebP на диск ):

Как вы можете видеть ниже WebPFree не очищает память, зарезервированную переменной типа IntPtr.Я не уверен, какой код должен быть добавлен в WebPFree и как очистить использованную память.Я также проверил этот пост , но также не нашел решения.

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
...