I am implementing custom connector with Code Preview feature.
I define the swagger like below:
swagger: '2.0'
info:
title: ImageAddWatermark
description: ''
version: '1.0'
host: api.meekou.cn
basePath: /
schemes:
- https
consumes: []
produces:
- application/json
paths:
/addwatermark:
post:
consumes:
- multipart/form-data
parameters:
- name: image
in: formData
type: file
required: true
- name: watermarktext
in: formData
type: string
required: true
responses:
default:
description: default
schema:
type: object
properties:
result:
type: string
description: result
summary: Add watermark to image
operationId: addwatermark
definitions: {}
parameters: {}
responses: {}
securityDefinitions: {}
security: []
tags: []
I want to read the image content in script like below:
public class Script : ScriptBase
{
public override async Task<HttpResponseMessage> ExecuteAsync()
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
try
{
// read content from request
var multipleFormParts = Context.Request.Content as MultipartContent;
var watermarkText = "";
var imageBase64 = "";
var result = "";
foreach (var dataContent in multipleFormParts)
{
switch (dataContent.Headers.ContentDisposition.Name)
{
case "watermarktext":
watermarkText = await dataContent.ReadAsStringAsync();
break;
case "image":
var fileBytes = await dataContent.ReadAsByteArrayAsync();
imageBase64 = Convert.ToBase64String(fileBytes);
break;
default:
break;
}
}
response.Content = CreateJsonContent(JsonConvert.SerializeObject(new { watermarktext = watermarkText, image = imageBase64, multipleFormParts = multipleFormParts }));
return response;
// add text to image
byte[] imageBytes = Convert.FromBase64String(imageBase64);
using (var ms = new MemoryStream(imageBytes))
using (var image = new Bitmap(ms))
{
using (var graphics = Graphics.FromImage(image))
{
using (var font = new Font("Arial", 24, FontStyle.Bold))
{
// Set the transparency level
int alpha = Math.Max(Math.Min(200, 255), 0);
// Create a color with the desired transparency level
Color transparentColor = Color.FromArgb(alpha, Color.White);
using (var brush = new SolidBrush(transparentColor))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
SizeF textSize = graphics.MeasureString(watermarkText, font);
float x = image.Width - textSize.Width - 2; // Adjust the X position of the watermark
float y = image.Height - textSize.Height - 2; // Adjust the Y position of the watermark
graphics.DrawString(watermarkText, font, brush, x, y);
}
}
}
using (var outputMs = new MemoryStream())
{
image.Save(outputMs, ImageFormat.Jpeg);
byte[] outputBytes = outputMs.ToArray();
result = Convert.ToBase64String(outputBytes);
}
}
response.Content = CreateJsonContent(JsonConvert.SerializeObject(new { result = result }));
}
catch (Exception ex)
{
response.Content = CreateJsonContent(JsonConvert.SerializeObject(new { result = JsonConvert.SerializeObject(ex) }));
}
return response;
}
}
But, it always return null for convert httpcontext to MultipartContent.
How to read the image paramter from PowerApps in "Script : ScriptBase"