2013-08-22 71 views
0

我在C#中編寫了一個簡單的圖像上傳器。這裏是我的代碼:簡單的圖像上傳器

所有的
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<byte[]> 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("<original_image>")).Replace("<original_image>", ""); 
       ORIGINIM = ORIGINIM.Substring(0, ORIGINIM.LastIndexOf("</original_image>")).Replace("</original_image>", ""); 

       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); 
     } 
    } 
} 

首先,在沒有Uploads.txt文件,它返回一個異常處理程序。然後第二次運行它,它創建它。有沒有更簡單的方法來存儲它?

第二個問題:

我能夠添加兩種不同的選擇,一個用於捕捉全屏,一個用於選擇屏幕區域。

我如何將它與我的代碼整合?你可以發佈嗎?

+0

當你說返回異常處理程序,你的意思是拋出異常?因爲根據你的代碼,它應該在第一次運行時創建文件。 – igelineau

+0

@igelineau是的,拋出異常。如果有任何暫時存放它的東西.. –

+0

什麼是例外? – mrtig

回答

0

代替(!File.Exists(@"Uploads.txt")) File.Create(@"Uploads.txt")試試這個

using (StreamWriter sw = new StreamWriter(File.Open(@"Uploads.txt", FileMode.OpenOrCreate))) 
{ 
    sw.WriteLine(ORIGINIM); 
} 

爲什麼它製作的第二次嘗試的原因是因爲你沒有用File.Create上創建文件上的鎖 - 現有的邏輯打開該文件,這將創建一個FileStream,然後您嘗試在同一個文件上打開StreamWriter。您應該通過將FileStream傳入其構造函數來創建StreamWriter,如上例所示。

+0

謝謝,它的工作原理。但無論如何不要在該文件中存儲任何內容?我已經創建了這個測試,供公衆使用,我不希望它被創建。它需要存儲嗎?如果不是,我怎麼能刪除它?我對這一切都很陌生。 –

+0

看起來文件部分與上傳沒有任何關係。嘗試將文件部分註釋掉。 – mrtig

+1

您可以使用'File.WriteAllText(「Uploads.txt」,ORIGINIM)縮短代碼;'' – igelineau