问题描述:

I want to capture a web image into a variable for processing (see below). I want to avoid saving it to a file, processing the file, then deleting the file. I can save the web-request output directly to a file, as shown, but if I save the output to a variable then save the variable content to a file, I do not have the same result.

 # saving output to a file works

$image='https://www.google.com/images/srpr/logo11w.png'

Invoke-WebRequest $image -outfile image1.png

<# I want to skip creating a file, using it, then deleting it

Save the image into a variable, convert to base64 to store in db, retrieve from db, convert back, save to a file

#>

Invoke-WebRequest $image -OutVariable imagevariable

$imagevariable | out-file image2.png # this is not a valid image

# Convert to base64 (using PSCX add-on) to store string

$toBase64=ConvertTo-Base64($imagevariable)

# save to, retrieve from db

#Invoke-Sqlcmd insert...

#Invoke-Sqlcmd select...

# Convert from base64 (using PSCX add-on) & output to a file

$fromBase64 | ConvertFrom-Base64 -OutputPath image3.png

For ease, I am going to use the PSCX community extensions for base64 conversion.

网友答案:

You are really close but the image bytes are in the returned object's Content property, try this:

$toBase64 = $imagevariable.Content | ConvertTo-Base64
相关阅读:
Top