title: Uploading an image using C# and WebRequest?
body: <p>Here is the working code in Python (using cURL):</p>
  
  <pre><code>#!/usr/bin/python
  
  import pycurl
  
  c = pycurl.Curl()
  values = [
            ("key", "YOUR_API_KEY"),
            ("image", (c.FORM_FILE, "file.png"))]
  # OR:     ("image", "http://example.com/example.jpg"))]
  # OR:     ("image", "BASE64_ENCODED_STRING"))]
  
  c.setopt(c.URL, "http://imgur.com/api/upload.xml")
  c.setopt(c.HTTPPOST, values)
  
  c.perform()
  c.close()
  </code></pre>
  
  <p>Here's what I have in C#:</p>
  
  <pre><code>public void UploadImage()
      {
          //I think this line is doing something wrong.
          //byte[] x = File.ReadAllBytes(@"C:\Users\Sergio\documents\visual studio 2010\Projects\WpfApplication1\WpfApplication1\Test\hotness2.jpg");
  
          //If I do it like this, using a direct URL everything works fine.
          string parameters = @"key=1b9189df79bf3f8dff2125c22834210903&amp;image=http://static.reddit.com/reddit.com.header.png"; //Convert.ToBase64String(x);
          WebRequest webRequest = WebRequest.Create(new Uri("http://imgur.com/api/upload"));
  
          webRequest.ContentType = "application/x-www-form-urlencoded";
          webRequest.Method = "POST";
          byte[] bytes = Encoding.ASCII.GetBytes(parameters);
  
          Stream os = null;
          try
          { // send the Post
              webRequest.ContentLength = bytes.Length;   //Count bytes to send
              os = webRequest.GetRequestStream();
              os.Write(bytes, 0, bytes.Length);         //Send it
          }
          catch (WebException ex)
          {
              MessageBox.Show(ex.Message, "HttpPost: Request error");
  
          }
          finally
          {
              if (os != null)
              {
                  os.Close();
              }
          }
  
          try
          { // get the response
              WebResponse webResponse = webRequest.GetResponse();
  
              StreamReader sr = new StreamReader(webResponse.GetResponseStream());
              MessageBox.Show(sr.ReadToEnd().Trim());
          }
          catch (WebException ex)
          {
              MessageBox.Show(ex.Message, "HttpPost: Response error");                  
          }            
  
      }
  </code></pre>
  
  <p>Now, the things I noticed is that when I changed my API key in the parameters string to "239231" or whatever number, the response I got was: "Invalid API key." So I think something must be working right.</p>
  
  <p>I placed my <strong>correct</strong> API key and now I get a different response: "Invalid image format. Try uploading a JPEG image."</p>
  
  <p>The service I'm using accepts almost every image format, so I am 100% certain the error is in the way I'm sending the file. Can anyone shed some light?</p>
  
  <p><strong>EDIT!!!</strong></p>
  
  <p>It turns out when I upload a JPG image I get that gray box thing. If I upload a big jpg image I don't get anything. For example: <a href="https://i.imgur.com/gFsUY.jpg" rel="nofollow noreferrer">http://i.imgur.com/gFsUY.jpg</a></p>
  
  <p>When I upload PNG's, the image uploaded doesn't even show.</p>
  
  <p>I'm certain the issue is the encoding. What can I do?</p>
  
  <p><strong>EDIT 2!!!</strong></p>
  
  <p>Now I'm 100% certain that the problem lies in the first line of the method. The File.ReadAllBytes() must be doing something wrong. If I upload a URL file, every works peachy: <a href="https://imgur.com/sVH61.png" rel="nofollow noreferrer">http://imgur.com/sVH61.png</a></p>
  
  <p>I wonder what encoding I should use. :S</p>