Skip to main content

Notifications

Power Automate - Connector Development
Unanswered

PowerApps custom connector Code Preivew read image file

(0) ShareShare
ReportReport
Posted on by

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"

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,422

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,711

Leaderboard

Featured topics