title: Processing a bitmap in a restful api c#
body: <p>i have a bitmap in which my bellow code returns the positions/coordinates of the dots in the image as follows:</p>
  
  <pre><code>public static void Main()
      {
          var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream("test.png");
  
          byte[] buffer = new byte[resource.Length];
          resource.Read(buffer, 0, buffer.Length);
  
          var positions = GetCoordinates(buffer);    
      }
  
      public static List&lt;Tuple&lt;int, int&gt;&gt; GetCoordinates(byte[] imgData)
      {
          int HEIGHT = 400;
          int WIDTH = 600;
  
          List&lt;Tuple&lt;int, int&gt;&gt; positions = new List&lt;Tuple&lt;int, int&gt;&gt;();
          Bitmap bitmap;
          using (Stream bmpStream = new MemoryStream(imgData))
          {
              Image image = Image.FromStream(bmpStream);
              bitmap = new Bitmap(image);
          }
  
          List&lt;int&gt; knownBlankColors = new List&lt;int&gt;();
          knownBlankColors.Add(Color.White.ToArgb());
  
          for (int x = 0; x &lt; HEIGHT; x++)
          {
              for (int y = 0; y &lt; WIDTH; y++)
              {
                  Color pixelColor = bitmap.GetPixel(x, y);
  
                  var code = pixelColor.ToArgb();
  
                  if (!knownBlankColors.Contains(code))
                      positions.Add(new Tuple&lt;int, int&gt;(x, y));       
              }  
          }
          return positions;
      }
  </code></pre>
  
  <p>this works well and gives the positions as follows </p>
  
  <blockquote>
    <p>(114, 33)
    (114, 34)
    (114, 35)
    (114, 36)
    (114, 37) etc etc</p>
  </blockquote>
  
  <p>Now my question is i am trying to convert the above process into a restful api so that the image gets passed via postman. This is what i tried</p>
  
  <pre><code> [Route("api/[controller]")]
  [ApiController]
  public class ProcessImageController : ControllerBase
  {
      public static IWebHostEnvironment _environment;
      public ProcessImageController(IWebHostEnvironment environment)
      {
          _environment = environment;
      }
  
      public class FileUploadApi
      {
          public IFormFile images { get; set; }
      }
      [HttpPost]
      public async Task&lt;string&gt; Post([FromForm]FileUploadApi objImages)
      {
          try
          {               
                  byte[] buffer = new byte[objImages.images.Length];                 
                  var positions = GetCoordinates(buffer);
                  return positions.ToString();
  
          }
          catch (Exception ex)
          {
              return ex.Message.ToString();
          }
      }
      public static List&lt;Tuple&lt;int, int&gt;&gt; GetCoordinates(byte[] imgData)
      {
          int HEIGHT = 400;
          int WIDTH = 600;
  
          List&lt;Tuple&lt;int, int&gt;&gt; positions = new List&lt;Tuple&lt;int, int&gt;&gt;();
          Bitmap bitmap;
          using (Stream bmpStream = new MemoryStream(imgData))
          {
              Image image = Image.FromStream(bmpStream);
              bitmap = new Bitmap(image);
          }
  
          List&lt;int&gt; knownBlankColors = new List&lt;int&gt;();
          knownBlankColors.Add(Color.White.ToArgb());
  
          for (int x = 0; x &lt; HEIGHT; x++)
          {
              for (int y = 0; y &lt; WIDTH; y++)
              {
                  Color pixelColor = bitmap.GetPixel(x, y);
  
                  var code = pixelColor.ToArgb();
  
                  if (!knownBlankColors.Contains(code))
                      positions.Add(new Tuple&lt;int, int&gt;(x, y));
  
  
              }
  
          }
  
          return positions;
      }
  }
  </code></pre>
  
  <p>is this the correct way of doing it? when i try to pass an image from postman it says invalid parameter. what am i doing wrong?</p>