2013-08-22 41 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