Powershell function for converting base64 WMF

DCAdmin 1 Reputation point
2020-10-23T14:03:41.507+00:00

Hello all,

I'm desperately searching for examples of powershell functions, that are able to convert base64 WMF strings to PNG. I've been fuzzling around for hours, and wasn't able to come even close to a working solution.

Could someone help out with a small example on how to decode a base64 string, convert it, and encode it again? It would be highly appreciated!

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,016 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. DCAdmin 1 Reputation point
    2020-10-23T16:28:17.713+00:00

    OK, just found out a reason why I cannot get the solution working. This is a (part of) the code I'm using. I ran the code also on an on-prem instance, and it worked flawlessly:

    # Input bindings are passed in via param block.
    param($Request, $TriggerMetadata)
    
    # Write to the Azure Functions log stream.
    Write-Host "PowerShell HTTP trigger function processed a request."
    
    # Interact with query parameters or the body of the request.
    $name = $Request.Body.Name
    $base64String = $Request.Body.base64String
    
    [System.IO.File]::WriteAllText($env:TEMP + "\SomeFile.txt", "My value") 
    
    Get-ChildItem -Path $env:TEMP 
    
    $bmp = [System.Drawing.Bitmap]::FromStream((New-Object System.IO.MemoryStream (@(, [Convert]::FromBase64String($base64String)))))
    
    $memory = New-Object System.IO.MemoryStream
    $null = $bmp.Save($env:TEMP + "\temp.png", [System.Drawing.Imaging.ImageFormat]::Png)
    $memory.Position = 0
    

    But when it runs in Azure Functions, it shows this error:

    "A generic error occurred in GDI+."
    

    I searched for it on the internet, and found some years old threads for on-prem problems, but none of them were solving my problem.

    Any solutions would be highly appreciated!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.