C#C
C#3y ago
Kaedrick

❔ Problem with image hosting API

Hello,
I tried to upload an image to an image hosting API but no matter what I do I get the following error :
System.Net.WebException: 'The remote server returned an error: (400) Bad Request.' (see screenshot).

Code :

C#
   MemoryStream stream = new MemoryStream(); // Stream that will contain the image bytes (can't upload an image straight to SQL db)
            productImage.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Png); // saves the image to byte in the object "stream"
            byte[] picture = stream.ToArray();
            string picture64 = Convert.ToBase64String(picture);

            using (WebClient wc = new WebClient()) // API pour upload l'image sur un serveur et foutre le lien dans la db
            {
                string URL = "https://thumbsnap.com/api/upload";
                string parameters = "key=censored&media=" + picture64;
                wc.Headers[HttpRequestHeader.ContentType] = "multipart/form-data";
                string htmlResult = wc.UploadString(URL, parameters);
            }
image.png
Was this page helpful?