近几天逆天经常大量复制粘贴一些图文信息,在某些特定的场合,图片都是无法直接粘贴进去的,就比如博客园的编辑器。
源码:https://github.com/dunitian/DNTLive/tree/master/Software/万恶剪贴板
要弄个什么还要把word里面的图片或者网上的图片先弄出来,这是多么的蛋疼啊~
于是就有了万恶的剪贴板==》为存储而生
其实是个很简单的功能点==》稍微说下核心代码
获取文本内容:var dataStr = Clipboard.GetText();
获取单个图片:var imgObj = Clipboard.GetImage();
获取图文格式(网页或者Word之类的):
var data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.Html, true))
{
return data.GetData(DataFormats.Html, true).ToString();
}正则匹配图片:Regex.Matches(dataStr, @"<img([^>]*)\s*src=('|\"")([^'\""]+)('|\"")", RegexOptions.ECMAScript)
异步批量下载图片:webClient.DownloadFileAsync(uri,path)
贴代码:
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
using
System;
using
System.Diagnostics;
using
System.IO;
using
System.Text;
using
System.Text.RegularExpressions;
using
System.Windows.Forms;
using
System.Net;
using
System.Drawing.Imaging;
namespace
剪贴板
{
public
partial
class
MainForm : Form
{
public
MainForm()
{
InitializeComponent();
}
/// <summary>
/// 获取文本
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
void
btnTxt_Click(
object
sender, EventArgs e)
{
var
dataStr = Clipboard.GetText();
if
(!
string
.IsNullOrEmpty(dataStr))
{
CreateDirectory(
"Text"
);
string
name =
string
.Format(
@"Text\{0}.txt"
, GetNewName());
File.WriteAllText(name, dataStr, Encoding.UTF8);
MessageBox.Show(
string
.Format(
"操作成功,请看Text文件夹!"
,
"逆天友情提醒"
));
OpenDirectory();
}
else
{
MessageBox.Show(
"剪贴板文本内容为空!"
,
"逆天友情提醒"
);
}
}
/// <summary>
/// 生成页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
void
btnPage_Click(
object
sender, EventArgs e)
{
var
dataStr = GetHtmlStr();
if
(!
string
.IsNullOrEmpty(dataStr))
{
MessageBox.Show(
"操作成功,请看打开的页面!"
,
"逆天友情提醒"
);
OutputHtml(dataStr);
}
else
{
MessageBox.Show(
"剪贴板图文内容为空!"
,
"逆天友情提醒"
);
}
}
/// <summary>
/// 生成文档
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
void
btnWord_Click(
object
sender, EventArgs e)
{
var
dataStr = GetHtmlStr();
if
(!
string
.IsNullOrEmpty(dataStr))
{
MessageBox.Show(
"操作成功,请看打开的页面!"
,
"逆天友情提醒"
);
OutputHtml(dataStr,
".doc"
);
}
else
{
MessageBox.Show(
"剪贴板图文内容为空!"
,
"逆天友情提醒"
);
}
}
/// <summary>
/// 导出图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
void
btnImg_Click(
object
sender, EventArgs e)
{
int
i = 0;
var
imgObj = Clipboard.GetImage();
var
dataStr = GetHtmlStr();
int
fileCount = GetFileDrop();
if
(imgObj !=
null
)
//非HTML的单张图片
{
CreateDirectory(
"Images"
);
imgObj.Save(
string
.Format(
@"Images\{0}.png"
, GetNewName()), ImageFormat.Png);
MessageBox.Show(
"操作成功,请看Images文件夹!"
,
"逆天友情提醒"
);
OpenDirectory();
}
else
if
(!
string
.IsNullOrEmpty(dataStr))
{
Stopwatch watch =
new
Stopwatch();
watch.Start();
i = DownloadImg(dataStr);
watch.Stop();
MessageBox.Show(
string
.Format(
"成功提取{0}个图片,耗时{1}。请查看Images文件夹"
, i, watch.Elapsed),
"逆天友情提醒"
);
OpenDirectory();
}
else
if
(fileCount > 0)
{
MessageBox.Show(
string
.Format(
"成功提取{0}个图片,请查看Images文件夹"
, fileCount),
"逆天友情提醒"
);
OpenDirectory();
}
else
{
MessageBox.Show(
"剪贴板图片信息为空!"
,
"逆天友情提醒"
);
}
}
/// <summary>
/// 本地图片-文件路径
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
private
int
GetFileDrop()
{
int
i = 0;
var
data = Clipboard.GetDataObject();
if
(data.GetDataPresent(DataFormats.FileDrop,
true
))
{
string
[] objs = (
string
[])data.GetData(DataFormats.FileDrop,
true
);
if
(objs !=
null
)
{
CreateDirectory(
"Images"
);
for
(
int
j = 0; j < objs.Length; j++)
{
File.Copy(objs[i], GetNewName());
i++;
}
}
}
return
i;
}
/// <summary>
/// 批量下载图片
/// </summary>
/// <param name="dataStr">页面字符串</param>
/// <param name="i">成功条数</param>
/// <returns></returns>
private
static
int
DownloadImg(
string
dataStr)
{
int
i = 0;
var
collection = Regex.Matches(dataStr,
@"<img([^>]*)\s*src=('|\"")([^'\""]+)('|\"")"
, RegexOptions.ECMAScript);
WebClient webClient =
new
WebClient();
foreach
(Match item
in
collection)
{
string
imgPath = item.Groups[3].Value;
try
{
CreateDirectory(
"Images"
);
webClient.DownloadFileAsync(
new
Uri(imgPath),
string
.Format(
@"Images\{0}.png"
, Path.GetFileName(imgPath)));
//剪贴板的图片没有相对路径
i++;
}
catch
(Exception ex) { File.WriteAllText(
"log.dnt"
, ex.ToString(), Encoding.UTF8); }
}
return
i;
}
/// <summary>
/// 清除剪贴板
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
void
lbl1_Click(
object
sender, EventArgs e)
{
ClearClipboard();
MessageBox.Show(
"剪贴板清除成功!"
,
"逆天友情提醒"
);
}
#region 公用方法
/// <summary>
/// HTML字符串
/// </summary>
/// <returns></returns>
private
static
string
GetHtmlStr()
{
var
data = Clipboard.GetDataObject();
if
(data.GetDataPresent(DataFormats.Html,
true
))
{
return
data.GetData(DataFormats.Html,
true
).ToString();
}
return
string
.Empty;
}
/// <summary>
/// 输出HTML文件
/// </summary>
/// <param name="dataStr"></param>
/// <param name="ext"></param>
private
static
void
OutputHtml(
string
dataStr,
string
ext =
".html"
)
{
CreateDirectory(
"Page"
);
string
name =
string
.Format(
@"Page\{0}{1}"
, GetNewName(), ext);
File.WriteAllText(name, dataStr.Substring(dataStr.IndexOf(
"<html"
)), Encoding.UTF8);
//除去版权信息
Process.Start(name);
}
/// <summary>
/// 打开目录
/// </summary>
private
static
void
OpenDirectory()
{
var
result = MessageBox.Show(
"是否打开文件夹?"
,
"逆天提醒"
, MessageBoxButtons.YesNo);
if
(result == DialogResult.Yes)
Process.Start(
"explorer.exe "
,
string
.Format(
@"{0}\images"
, Directory.GetCurrentDirectory()));
//打开目录
}
/// <summary>
/// 生成新名称-就不用 Guid 了,普通用户看见了会怕
/// </summary>
/// <returns></returns>
private
static
string
GetNewName()
{
return
DateTime.Now.ToString(
"yyyy-MM-dd~HH.mm.ss.fff"
);
}
/// <summary>
/// 创建文件夹
/// </summary>
private
static
void
CreateDirectory(
string
name)
{
if
(!Directory.Exists(name))
{
Directory.CreateDirectory(name);
}
}
/// <summary>
/// 清除剪贴板
/// </summary>
private
void
ClearClipboard()
{
Clipboard.Clear();
}
#endregion
}
}
|
源码地址:http://pan.baidu.com/s/1b0ajxW
备用地址:https://github.com/dunitian/DNTLive/tree/master/Software/万恶剪贴板
本文转自毒逆天博客园博客,原文链接:http://www.cnblogs.com/dunitian/p/5377031.html,如需转载请自行联系原作者