Analyze URL image

0

How can I do to instead send a parse to the local it parse an image from a remote address or a url. For if I try to replace the address: C: \ Users \ madureira \ Downloads \ JRMJ.jpg by a URL the error message appears: 'The given path format is not supported.'

Code:

  protected void Button1_Click(object sender, EventArgs e)
{
    string imageFilePath = @"C:\Users\madureira\Downloads\JRMJ.jpg";
    Literal1.Text= ObterEmocoes(imageFilePath);
}


  public async Task<string> ObterEmocoes(string imageFilePath)
    {
        HttpResponseMessage respostaHttp;
        string json;

        byte[] byteData = GetImageAsByteArray(imageFilePath);

        string url = "https://brazilsouth.api.cognitive.microsoft.com/face/v1.0/detect";

        string queryString = "returnFaceId=true&returnFaceLandmarks=false" +
            "&returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses," +
            "emotion,hair,makeup,occlusion,accessories,blur,exposure,noise";

        var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "<<key>>");
        using (ByteArrayContent content = new ByteArrayContent(byteData))
        {
            content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            respostaHttp = await httpClient.PostAsync($"{url}?{queryString}", content);
            json = await respostaHttp.Content.ReadAsStringAsync();
        }
        return json;
    }

    static byte[] GetImageAsByteArray(string imageFilePath)
    {
        using (FileStream fileStream =
            new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
        {
            BinaryReader binaryReader = new BinaryReader(fileStream);
            return binaryReader.ReadBytes((int)fileStream.Length);
        }
    }
    
asked by anonymous 29.08.2018 / 04:35

1 answer

1

If what you submit is a byte[] and you want to get it from an external url too, add this functionality to the GetImageAsByteArray() method or create a new one specific to it.

static byte[] GetImageAsByteArrayFromUrl(string imageUrlPath)
{
    byte[] byteData = null;

    using (var client = new System.Net.WebClient())
    {
        var url = new Uri(imageUrlPath);
        byteData = client.DownloadData(url);
    }

    return byteData;
}
    
29.08.2018 / 17:26