Search code examples
c#azureazure-functionsimageresizer

How do I apply grayscale to my image with SimpleFilters in C#?


I am creating a function in Azure that takes an image and resizes it + makes it grayscale. I'm currently using this function:

#r "System.Drawing"

using ImageResizer;
using ImageResizer.Plugins.SimpleFilters;
using System.Drawing;
using System.Drawing.Imaging;

public static void Run(Stream inputImage, string imageName, Stream 
resizedImage, TraceWriter log)
{
  log.Info($"C# Blob trigger function Processed blob\n Name:{imageName} \n 
  Size: {inputImage.Length} Bytes");

  var settings = new ImageResizer.ResizeSettings{
    MaxWidth = 400,
    Format = "png"
  };

  // Add the grayscale filter to the image
  inputImage.filters.Add(GrayscaleNTSC());

  ImageResizer.ImageBuilder.Current.Build(inputImage, resizedImage, settings);

}

I'm importing the Plugins.SimpleFilters but I don't know how to use it in C#. The project site provides examples in pure HTML.

Do you know how to grayscale the image?

I get the following error: The name 'GrayscaleNTSC' does not exist in the current context

The packages I'm using are:

"dependencies": {
  "ImageResizer": "4.0.5",
  "ImageResizer.Plugins.SimpleFilters": "4.0.5"
}

Solution

  • According to your description, I assume that you could leverage Managed API from ImageResizer to achieve your purpose. Here is my test, you could refer to it:

    Core code

    //install the SimpleFilters plugin
    ImageResizer.Configuration.Config.Current.Plugins.Install(new SimpleFilters());
    var settings = new ResizeSettings("width=400&height=500&crop=auto&s.grayscale=true");
    ImageBuilder.Current.Build($"{baseDir}/resources/image_01.jpg", $"{baseDir}/resources/image_01_01.jpg",
                        settings);
    

    Result

    enter image description here

    Additionally, you could refer to Managed API samples and SimpleFilters plugin for more details.