根据文字生成图片
本文关键字: c#, 文字生成图片下面这个应用代码主要用于模拟POST请求后返回结果,然后正则匹配可能出现的文字或图片,如果是文字则生成图片,如果是图片则直接下。关键代码如下:
Regex reg = new Regex(@”<font style=””FONT-SIZE: 26pt;””>(?<mycontent>.*?)<\/font>”);
stringDic[“test”]=”0″;
MatchCollection match=reg.Matches(stringDic[“backhtml”]);listDic[“testlist”].Clear();
foreach(Match mt in match)
{Random ran = new Random(GetRandomSeed());
string RandKey = ran.Next(0, 99999999).ToString().PadLeft(8,’0′);
string filename=DateTime.Now.Ticks.ToString()+RandKey+”.gif”;try
{
if (!Directory.Exists(stringDic[“rootpath”]+@”:\\Temp\\erfenzi\\”+stringDic[“关键词”]+”\\”))//如果不存在就创建 dir 文件夹
Directory.CreateDirectory(stringDic[“rootpath”]+@”:\\Temp\\erfenzi\\”+stringDic[“关键词”]+”\\”);
}
catch{}listDic[“testlist”].Add(filename);
if(mt.Groups[“mycontent”].Value.ToString().Contains(“<img”))
{
Regex reg2=new Regex(@”<img src=(?<mypic>[^ ]*?) width=30>”);try{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;//.Ssl3;//.Tls12; //加上这一句
System.Net.WebRequest webreq = System.Net.WebRequest.Create(“http://XXXXXXX.com/”+reg2.Match(mt.Groups[“mycontent”].Value.ToString()).Groups[1].Value.ToString());
System.Net.WebResponse webres = webreq.GetResponse();
Stream stream = webres.GetResponseStream();Image.FromStream(stream).Save(stringDic[“rootpath”]+@”:\\Temp\\erfenzi\\”+stringDic[“关键词”]+”\\”+filename);
}
catch{}
}
else
{
string text =System.Net.WebUtility.HtmlDecode(mt.Groups[“mycontent”].Value.ToString().Trim());//得到Bitmap(传入Rectangle.Empty自动计算宽高)
try{
Bitmap bmp = TextToBitmap(text,new Font(new FontFamily(“微软雅黑”), 52), Rectangle.Empty, Color.Black, Color.White);//保存到桌面save.jpg
string directory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory);
bmp.Save(stringDic[“rootpath”]+@”:\\Temp\\erfenzi\\”+stringDic[“关键词”]+”\\”+filename, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch{}
}
}stringDic[“test”]=match.Count>0?”yes”:”no”;