Search code examples
c#imageazure-functionsazure-blob-storagepowerapps

How to implement My Azure function for using it in PowerApps


Im currently trying to write a function to combine two images from azure storage to one image. I dont know how to setup my function class so that I can trigger the function in powerapps with 2 selected images.

This is my class

    public static class Function1
{
    [Obsolete] // switch TraceWrite to Ilogger
    [FunctionName("Function1")]
    public static void Run(string background, string overlay, CloudBlockBlob outputBlob, TraceWriter log)
    {
        log.Info($"Function triggered with blob\n Background: {background} \n Overlay: {overlay}");

        ConvertMe Converter = new ConvertMe(System.Drawing.Color.Black); // My ImageEdit Class I have written
        Bitmap _Main = new Bitmap(background);
        Bitmap Overlay = Converter.MakeTransparent(overlay);



        using (MemoryStream memory = new MemoryStream())
        {
            Converter.ComebineBitmap(_Main, Overlay).Save(memory, ImageFormat.Jpeg);
            memory.Position = 0;
            outputBlob.Properties.ContentType = "image/jpeg";
            outputBlob.UploadFromStreamAsync(memory);
        }


    }

}

Solution

  • Firstly, suppose you already know you could not directly call your function, especially you are using blob trigger function.

    Then is about how to use function in power apps. There is blog about this: Using Azure Functions in PowerApps. You need the http trigger function and define the REST signature using Swagger then use the custom API in power apps.

    The last thing is about how to get two blob in the http trigger function. From the blob binding doc you could get the Input-usage, you could find the c# or c# script function both support CloudBlockBlob binding.

    The below is a sample read from two txt blob with http trigger function, you could add an output binding to storage the output image.

    public static class Function1
        {
            [FunctionName("Function1")]
            public static async Task RunAsync(
                [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
                [Blob("test/test.txt",FileAccess.ReadWrite)]CloudBlockBlob blob1,
                [Blob("test/out.txt", FileAccess.ReadWrite)]CloudBlockBlob blob2,
                ILogger log)
            {
                log.LogInformation("C# HTTP trigger function processed a request.");
    
                string test = await blob1.DownloadTextAsync();
                string outtxt = await blob2.DownloadTextAsync();
                log.LogInformation("test value: " + test);
                log.LogInformation("outtxt value: " + outtxt);
    
            }
        }
    

    enter image description here

    Then follow the blog, suppose this could work, hope this could help you, if you still have other problem, please feel free to let me know.