title: simple image uploader
body: <p>I wrote a simple image uploader in C#. Here's my code:</p>
  
  <pre><code>using System;
  using System.Collections.Specialized;
  using System.Drawing;
  using System.Drawing.Imaging;
  using System.IO;
  using System.Net;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.Xml.Linq;
  
  namespace Snappx
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
              GlobalHook.HookManager.KeyUp += new KeyEventHandler(MyKeyUp);
              CheckForIllegalCrossThreadCalls = false;
              new Task(this.Hide).Start();
          }
  
          private void exitToolStripMenuItem_Click(object sender, EventArgs e)
          {
              Environment.Exit(-1);
          }
  
          string ORIGINIM;
          async void MyKeyUp(object sender, KeyEventArgs e)
          {
              if (e.KeyCode == Keys.PrintScreen)
              {
                  await GetImage();
                  e.Handled = true;
              }
              else e.Handled = false;
          }
  
          String img = @"temp";
          async Task GetImage()
          {
              Rectangle bounds = Screen.GetBounds(Point.Empty);
              using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
              {
                  using (Graphics g = Graphics.FromImage(bitmap))
                  {
                      g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
                  }
                  bitmap.Save(img, ImageFormat.Png);
              }
  
              using (var w = new WebClient())
              {
                  var values = new NameValueCollection { { "key", "85684005b7d4faa4c33ee480010d4982" }, { "image", Convert.ToBase64String(File.ReadAllBytes(img)) } };
  
                  notifyIcon1.ShowBalloonTip(3, "Uploading", "Uploading image to Imgur", ToolTipIcon.Info);
  
                  w.UploadProgressChanged += new UploadProgressChangedEventHandler(wc_UploadProgressChanged);
                  Task&lt;byte[]&gt; x = w.UploadValuesTaskAsync(new Uri("http://imgur.com/api/upload.xml"), values);
                  byte[] response = await x;
  
                  while (w.IsBusy) System.Threading.Thread.Sleep(500);
                  File.Delete(img);
  
                  ORIGINIM = Convert.ToString(XDocument.Load(new MemoryStream(response)));
                  ORIGINIM = ORIGINIM.Substring(ORIGINIM.LastIndexOf("&lt;original_image&gt;")).Replace("&lt;original_image&gt;", "");
                  ORIGINIM = ORIGINIM.Substring(0, ORIGINIM.LastIndexOf("&lt;/original_image&gt;")).Replace("&lt;/original_image&gt;", "");
  
                  Clipboard.SetText(ORIGINIM);
                  if (!File.Exists(@"Uploads.txt")) File.Create(@"Uploads.txt");
                  new StreamWriter(@"Uploads.txt").WriteLine(ORIGINIM);
                  notifyIcon1.ShowBalloonTip(3, "Done", "URL copied to clipboard.", ToolTipIcon.Info);
              }
          }
  
          private void wc_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
          {
              int percentage = e.ProgressPercentage * 2;
              notifyIcon1.ShowBalloonTip(3, "Uploading", (percentage).ToString() + "%", ToolTipIcon.Info);
          }
      }
  }
  </code></pre>
  
  <p>First of all, when there is no Uploads.txt file, it returns an Exception handler. Then second time I run it, it creates it. Is there any simpler way to store it?</p>
  
  <p><strong>Second question:</strong></p>
  
  <p>Am I able to add two different options, one for capture full screen and one for select screen region.</p>
  
  <p>How could I integrate it with my code? Could you post it?</p>