private void uploadButton_Click(object sender, EventArgs e)
{
try
{
uploadButton.Text = "Uploading...";
uploadButton.Enabled = false;
using (WebClient w = new WebClient())
using (MemoryStream ms = new MemoryStream())
{
Image outputImage = CreateOutputImage();
outputImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
var values = new NameValueCollection
{
{ "image", Convert.ToBase64String(ms.ToArray()) }
};
w.Headers.Add("Authorization", "Client-ID c388ed6b11fecdb");
byte[] response = w.UploadValues("https://api.imgur.com/3/image", values);
JObject result = (JObject)JsonConvert.DeserializeObject(System.Text.Encoding.ASCII.GetString(response));
string link = result["data"]["link"].ToString();
linkBox.Text = link;
linkBox.Visible = true;
goButton.Visible = true;
}
}
catch (Exception ex)
{
MessageBox.Show("Upload failed!\nReason: " + ex.Message);
}
finally
{
uploadButton.Text = "Upload to Imgur";
uploadButton.Enabled = true;
}
}