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 ファイルがない場合は、Exception ハンドラを返します。次に、2回目に実行すると、作成されます。もっと簡単に保管する方法はありますか?

2 番目の質問:

全画面キャプチャ用と画面領域選択用の 2 つの異なるオプションを追加できますか。

どうすればそれを自分のコードに統合できますか? 投稿していただけますか?

4

1 に答える 1