Image Converter

FREEMIUM
By petadata | Updated il y a 25 jours | Video, Images
Popularity

8.3 / 10

Latency

739ms

Service Level

100%

Health Check

N/A

Back to All Tutorials (5)

Convert Any Image to WebP by using Visual Basic

In this tutorial we will cover how to implement asynchronous approach of Image Converter. You only need to send source image to SubmitWebPConversionTask or any other submission API methods like SubmitBmpConversionTask, SubmitDicomConversionTask etc. to receive task identifier as response.

After receiving task identifier you need to check task status by calling GetConversionTaskStatus method. You need to call same method after a few seconds delay as long as task status equals to “Waiting”. Once you receive “Completed” from GetConversionTaskStatus method you can call DownloadResult method to download final image. You can use the Visual Basic implementation below.

Imports System
Imports System.IO
Imports System.Net.Http

Module Program
    Async Function SubmitWebPConversionTask(filePathToConvert As String, rapidApiKey As String) As Task(Of String)
        Using httpClient As New HttpClient()
            Using form As New MultipartFormDataContent()

                form.Headers.Add("X-RapidAPI-Key", rapidApiKey)
                form.Add(New StringContent("true"), "lossy")
                Dim fileBytes = Await File.ReadAllBytesAsync(filePathToConvert)
                Dim fileName = Path.GetFileName(filePathToConvert)
                form.Add(New ByteArrayContent(fileBytes, 0, fileBytes.Length), "file", fileName)

                Using response = Await httpClient.PostAsync("https://image-converter4.p.rapidapi.com/submitWebPConversionTask", form)

                    response.EnsureSuccessStatusCode()
                    Return Await response.Content.ReadAsStringAsync()
                End Using
            End Using
        End Using
    End Function
    Async Function GetConversionTaskStatus(taskId As String, rapidApiKey As String) As Task(Of String)
        Using httpClient As New HttpClient()
            httpClient.DefaultRequestHeaders.Add("X-RapidAPI-Key", rapidApiKey)
            Using response = Await httpClient.GetAsync($"https://image-converter4.p.rapidapi.com/getConversionTaskStatus?taskId={taskId}")

                response.EnsureSuccessStatusCode()
                Return Await response.Content.ReadAsStringAsync()
            End Using
        End Using
    End Function
    Async Function DownloadResult(taskId As String, rapidApiKey As String) As Task(Of Byte())
        Using httpClient As New HttpClient()
            httpClient.DefaultRequestHeaders.Add("X-RapidAPI-Key", rapidApiKey)
            Using response = Await httpClient.GetAsync($"https://image-converter4.p.rapidapi.com/downloadResult?taskId={taskId}")

                response.EnsureSuccessStatusCode()
                Return Await response.Content.ReadAsByteArrayAsync()
            End Using
        End Using
    End Function
    Public Async Function ConvertToWebPAsync() As Task
        Dim rapidApiKey = "<YOUR RAPIDAPI KEY HERE>"
        Dim filePathToConvert = "<SOURCE IMAGE FILE PATH TO CONVERT>"
        Dim taskId = Await SubmitWebPConversionTask(filePathToConvert, rapidApiKey)
        Dim retryCount = 0
        Do While (retryCount < 100)
            retryCount += 1
            Await Task.Delay(5000)
            Dim status = Await GetConversionTaskStatus(taskId, rapidApiKey)
            If (status = "Completed") Then
                Dim fileBytes = Await DownloadResult(taskId, rapidApiKey)
                Await File.WriteAllBytesAsync("final_image.webp", fileBytes)
                Exit Do
            ElseIf (status = "Waiting") Then
                Continue Do
            ElseIf (status = "Failed") Then
                Throw New Exception("Cannot convert image")
            Else
                Throw New Exception("Invalid status")
            End If
        Loop
    End Function
    Sub Main(args As String())
        ConvertToWebPAsync().Wait()
    End Sub
End Module