1

I have a problem, I want to convert an image from a url to base64, but I get an error

This is the url: https://www.jotform.com/widget-uploads/imagePreview/92613577901663/2922362Principios%20-%20Resultados.png

    public String ConvertImageURLToBase64(String url)
    {
        StringBuilder sb = new StringBuilder();

        Byte[] vbyte = this.GetImage(url);

        sb.Append(Convert.ToBase64String(vbyte, 0, vbyte.Length));

        return sb.ToString();
    }

    private byte[] GetImage(string url)
    {
        Stream stream = null;
        byte[] buf;

        try
        {
            WebProxy myProxy = new WebProxy();
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            stream = response.GetResponseStream();

            using (BinaryReader br = new BinaryReader(stream))
            {
                int len = (int)(response.ContentLength);
                buf = br.ReadBytes(len);
                br.Close();
            }

            stream.Close();
            response.Close();
        }
        catch (Exception exp)
        {
            buf = null;
        }

        return (buf);
    }
stvn03
  • 43
  • 2
  • 2
  • 5

2 Answers2

1

You appear to be using the Content-Length response from a server that doesn't respond with one, so as a result you're using the default value of -1 to create an array. This is failing with the following exception:

Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.dll
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Non-negative number required.

You can fix this by ignoring the content length returned from the server, and reading all of the data into memory till the response is done:

byte[] GetImage(string url)
{
    Stream stream = null;
    byte[] buf;

    try
    {
        WebProxy myProxy = new WebProxy();
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

        HttpWebResponse response = (HttpWebResponse)req.GetResponse();
        stream = response.GetResponseStream();

        using (MemoryStream ms = new MemoryStream())
        {
            stream.CopyTo(ms);
            buf = ms.ToArray();
        }

        stream.Close();
        response.Close();
    }
    catch (Exception exp)
    {
        buf = null;
    }

    return (buf);
}

And for future reference: Debugging these sorts of errors is much easier if you display the exception you see in your try / catch block, so you have some idea what the error is.

Anon Coward
  • 9,784
  • 3
  • 26
  • 37
  • 1
    Remember to dispose of the `stream` and `response` objects (don't just `Close()` them). Or declare them all with `using` statements like the `MemoryStream` object (which, btw, is the only one that you may forget to dispose of). – Jimi Oct 14 '19 at 23:02
0

This is the error that appears in the execution: Error Msg

stvn03
  • 43
  • 2
  • 2
  • 5