C# 简单图像上传器

C# 简单图像上传器,c#,.net,C#,.net,我用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; na

我用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);
        }
    }
}
使用系统;
使用System.Collections.Specialized;
使用系统图;
使用系统、绘图、成像;
使用System.IO;
Net系统;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Xml.Linq;
名称空间Snappx
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
GlobalHook.HookManager.KeyUp+=新的KeyEventHandler(MyKeyUp);
CheckForIllegalCrossThreadCalls=false;
新任务(this.Hide).Start();
}
private void exitToolStripMenuItem\u单击(对象发送者,事件参数e)
{
环境。退出(-1);
}
弦原音;
异步void MyKeyUp(对象发送方,KeyEventArgs e)
{
if(e.KeyCode==Keys.PrintScreen)
{
等待GetImage();
e、 已处理=正确;
}
否则e.已处理=错误;
}
字符串img=@“temp”;
异步任务GetImage()
{
矩形边界=Screen.GetBounds(Point.Empty);
使用(位图位图=新位图(bounds.Width、bounds.Height))
{
使用(Graphics g=Graphics.FromImage(位图))
{
g、 CopyFromScreen(Point.Empty,Point.Empty,bounds.Size);
}
保存(img,ImageFormat.Png);
}
使用(var w=new WebClient())
{
var values=new NameValueCollection{{“key”,“85684005B7D4FAA4C33EE48001D4982},{“image”,Convert.ToBase64String(File.ReadAllBytes(img))};
notifyIcon1.showBallootTip(3,“上载”,“将图像上载到Imgur”,ToolTipIcon.Info);
w、 UploadProgressChanged+=新的UploadProgressChangedEventHandler(wc\U UploadProgressChanged);
任务x=w.UploadValuesTaskAsync(新Uri(“http://imgur.com/api/upload.xml价值观);
字节[]响应=等待x;
while(w.IsBusy)系统线程线程睡眠(500);
文件删除(img);
ORIGINIM=Convert.ToString(XDocument.Load(新内存流(响应));
ORIGINIM=ORIGINIM.Substring(ORIGINIM.LastIndexOf(“”)。替换(“,”);
ORIGINIM=ORIGINIM.Substring(0,ORIGINIM.LastIndexOf(“”)。替换(“,”);
剪贴板.SetText(ORIGINIM);
如果(!File.Exists(@“Uploads.txt”))File.Create(@“Uploads.txt”);
新的StreamWriter(@“Uploads.txt”).WriteLine(ORIGINIM);
notifyIcon1.showBallootTip(3,“完成”,“URL复制到剪贴板”,ToolTipIcon.Info);
}
}
私有void wc_UploadProgressChanged(对象发送方,UploadProgressChangedEventArgs e)
{
整数百分比=e.ProgressPercentage*2;
notifyIcon1.showBallootTip(3,“上载”(百分比).ToString()+“%”,ToolTipIcon.Info);
}
}
}
首先,当没有Uploads.txt文件时,它会返回一个异常处理程序。然后第二次运行它时,它会创建它。有没有更简单的存储方法

第二个问题:

我是否能够添加两个不同的选项,一个用于捕获全屏,另一个用于选择屏幕区域

如何将其与代码集成?你能发布它吗?

而不是
(!File.Exists(@“Uploads.txt”))文件。创建(@“Uploads.txt”)
试试这个

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

它在第二次尝试中起作用的原因是,您没有使用
file.create
在文件上创建锁-现有逻辑打开文件,创建
FileStream
,然后您尝试在同一文件上打开
StreamWriter
。您应该通过将
FileStream
传递到其构造函数中来创建
StreamWriter
,如上例所示。

当您说返回异常处理程序时,您的意思是抛出异常吗?因为根据您的代码,它应该在第一次运行时创建文件。@igelineau是的,抛出一个异常。如果有任何临时存储或其他的方法……有什么例外吗?@mrtig谢谢,它可以工作。但是,有没有办法不在该文件中存储任何内容?我创建它只是为了测试,为了公共用途,我不希望它被创建。需要储存吗?如果没有,我如何删除它?我对这一切都很陌生。看起来文件部分与上传没有任何关系。用注释掉的文件部分试试。你可以使用
file.writealText(“Uploads.txt”,ORIGINIM)来缩短代码