-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
121 lines (109 loc) · 3.77 KB
/
Form1.cs
File metadata and controls
121 lines (109 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System.Diagnostics;
using System.Drawing;
using System.Net;
using ZXing.Windows.Compatibility;
namespace WinFormsAppQRCode
{
public partial class Form1 : Form
{
private readonly HttpClient httpClient = new HttpClient();
private readonly BarcodeReader barcodeReader = new BarcodeReader();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Process process = new Process()
{
StartInfo = new ProcessStartInfo()
{
UseShellExecute = true,
FileName = button1.Text,
}
};
process.Start();
}
private void button1_TextChanged(object sender, EventArgs e)
{
if (button1.Text.Length > 0)
{
button1.Enabled = true;
}
else
{
button1.Enabled = false;
}
}
private void Form1_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.V)
{
if (Clipboard.ContainsData(DataFormats.Bitmap))
{
pictureBox1.Image = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap);
button1.Text = DecodeQRCode(pictureBox1.Image);
}
}
}
private string DecodeQRCode(Image image)
{
return barcodeReader.Decode((Bitmap)image)?.Text ?? string.Empty;
}
private async void Form1_DragDrop_1(object sender, DragEventArgs e)
{
try
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files.Length == 1)
{
pictureBox1.Image = Image.FromFile(files[0]);
button1.Text = DecodeQRCode(pictureBox1.Image);
return;
}
else
{
throw new Exception("Only max 1");
}
}
else if (e.Data.GetDataPresent(DataFormats.Text))
{
string url = e.Data.GetData(DataFormats.Text).ToString();
byte[] imageData = await httpClient.GetByteArrayAsync(url);
pictureBox1.Image = Image.FromStream(new MemoryStream(imageData));
button1.Text = DecodeQRCode(pictureBox1.Image);
return;
}
//else if(e.Data.GetDataPresent(DataFormats.Bitmap))
//{
// pictureBox1.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
//}
else
{
throw new Exception("Not filedrop or text");
}
}
catch (Exception ex)
{
pictureBox1.Image?.Dispose();
button1.Text = string.Empty;
MessageBox.Show(ex.ToString());
}
}
private void Form1_DragEnter_1(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop) || e.Data.GetDataPresent(DataFormats.Text)
|| e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Link;
//copy thì được kéo ảnh ở photo vào.
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
httpClient.Dispose();
}
}
}