62,046
社区成员
发帖
与我相关
我的任务
分享
//保存图片到本地并转换成水印图片
public static void savepic(string ohtml,string syimgPath, string imgurlPath, string tempPath)
{
//Response.Write(ohtml);
System.Text.RegularExpressions.MatchCollection m;
//提取字符串的图片
m = Regex.Matches(ohtml.ToLower(), "(<img).*?>");
for (int i = 0; i < m.Count; i++)
{
string ostr = m[i].ToString();
//提取图片的地址
System.Text.RegularExpressions.MatchCollection m2;
//以下的正则只能替换图片地址有双引号的
m2 = Regex.Matches(ostr, "(src=)['|\"].*?['\"]");
//picname = Guid.NewGuid().ToString();
for (int j = 0; j < m2.Count; j++)
{
string strTemp = m2[j].ToString();
strTemp = strTemp.Replace("src=", "");
strTemp = strTemp.Replace("\"", "");
strTemp = strTemp.Replace("'", "");
strTemp = strTemp.Replace("@", "");
strTemp = strTemp.Replace("v=0", "");
strTemp = strTemp.Replace("?", "");
//strTemp = strTemp.Replace("&", "&");
//Response.Write(strTemp+ "<br>");
//Response.Write(m2.ToString());
System.Net.WebClient wc = new System.Net.WebClient();
//在这里检查路径
bool a = GetUrlError(strTemp);
if (a == true)
{
//图片名称
string picname = Path.GetFileName(strTemp);
string hz = getImgHz(strTemp);//后缀
string picnameex = Path.GetExtension(strTemp);
string webFilePath_syyt = syimgPath; // 服务器端水印图路径(图片)
if (picnameex.Length == 0)
{
string tempPath2 = null;
//string nn = imgurlPath + picname + ".jpg";
tempPath2 = tempPath + picname + hz;
System.Net.ServicePointManager.DefaultConnectionLimit = 500;
//下载图片,还需要解决“System.Net.WebException: 操作已超时。”问题
wc.DownloadFile(strTemp, tempPath2);
}
else
{
string tempPath2 = null;
//string nn = imgurlPath + picname + ".jpg";
tempPath2 = tempPath + picname;
//下载图片
wc.DownloadFile(strTemp, tempPath2);
}
}
}
}
}