I'm currently writing a function that, when executed, calls an API which returns a PNG image. I want to then take that image. Convert it to a JPEG and URL encode it before returning it as a response from the function.
I'm relatively new when working with Azure function, so directions for good pieces of documentation would be greatly appreciated.
private static string _websiteurl = "https://studentSystem/";
[FunctionName("Function1")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
{
var endPointString = "/v1/students/12162333/id-card-photos";
using var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/png"));
var url = new Uri(_websiteurl + endPointString);
var response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
return null //Jpeg as URL encoded
}
Does anyone know of a good way to directly do this just in memory? I can think of a way that involves saving the file and then reading it again but that doesn't seem to be very efficient and I would like to avoid it if possible. Is converting it to jpeg then URL encoding better then the other way round?
Regards.
I Solved it using System.Drawing.Image
and the memoryStream
class.
using var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/png"));
// replace Url with your URL
var response = await client.GetAsync(WebsiteUrl);
response.EnsureSuccessStatusCode();
string base64String = null;
await using (var stream = new MemoryStream())
{
var image = Image.FromStream(await response.Content.ReadAsStreamAsync());
image.Save(stream, ImageFormat.Jpeg);
stream.Position = 0;
base64 = Convert.ToBase64String(stream.ToArray());
}
//
return new OkObjectResult(base64);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.