0

i have a bitmap in which my bellow code returns the positions/coordinates of the dots in the image as follows:

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<Tuple<int, int>> GetCoordinates(byte[] imgData)
    {
        int HEIGHT = 400;
        int WIDTH = 600;

        List<Tuple<int, int>> positions = new List<Tuple<int, int>>();
        Bitmap bitmap;
        using (Stream bmpStream = new MemoryStream(imgData))
        {
            Image image = Image.FromStream(bmpStream);
            bitmap = new Bitmap(image);
        }

        List<int> knownBlankColors = new List<int>();
        knownBlankColors.Add(Color.White.ToArgb());

        for (int x = 0; x < HEIGHT; x++)
        {
            for (int y = 0; y < WIDTH; y++)
            {
                Color pixelColor = bitmap.GetPixel(x, y);

                var code = pixelColor.ToArgb();

                if (!knownBlankColors.Contains(code))
                    positions.Add(new Tuple<int, int>(x, y));       
            }  
        }
        return positions;
    }

this works well and gives the positions as follows

(114, 33) (114, 34) (114, 35) (114, 36) (114, 37) etc etc

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

 [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<string> 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<Tuple<int, int>> GetCoordinates(byte[] imgData)
    {
        int HEIGHT = 400;
        int WIDTH = 600;

        List<Tuple<int, int>> positions = new List<Tuple<int, int>>();
        Bitmap bitmap;
        using (Stream bmpStream = new MemoryStream(imgData))
        {
            Image image = Image.FromStream(bmpStream);
            bitmap = new Bitmap(image);
        }

        List<int> knownBlankColors = new List<int>();
        knownBlankColors.Add(Color.White.ToArgb());

        for (int x = 0; x < HEIGHT; x++)
        {
            for (int y = 0; y < WIDTH; y++)
            {
                Color pixelColor = bitmap.GetPixel(x, y);

                var code = pixelColor.ToArgb();

                if (!knownBlankColors.Contains(code))
                    positions.Add(new Tuple<int, int>(x, y));


            }

        }

        return positions;
    }
}

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?

marry
  • 153
  • 2
  • 3
  • 15
  • Converting the exception to a string and returning it is a bad idea. How will the caller know if the returned string is the coordinates or an error message? You're better off not catching the exception and letting the framework return a HTTP error code. – Sean Mar 13 '20 at 10:10
  • @Sean noted. but currently its not returning the coordinates in the api.am i doing the process incorrectly – marry Mar 13 '20 at 10:14
  • Can you check this: https://stackoverflow.com/questions/28369529/how-to-set-up-a-web-api-controller-for-multipart-form-data – Oguz Ozgul Mar 13 '20 at 10:22
  • @OguzOzgul but im not uploading the file from a directory. its going to be passed from a web app, therefor was using postman to test the image passed – marry Mar 13 '20 at 11:58

0 Answers0