private void BuscaCaptchaString() { var text = wb.Document.GetElementById("cipCaptchaImg"); var html = String.Format("{0}{1}{2}", "<table cellpadding=\"0\" cellspacing=\"0\">", text.InnerHtml, "</table>"); var htmlToImageConv = new NReco.ImageGenerator.HtmlToImageConverter(); // htmlToImageConv.Height = 70; htmlToImageConv.Width = 200; var jpegBytes = htmlToImageConv.GenerateImage(html, "Png"); piccaptcha.Image = ByteArrayToImage(jpegBytes); Bitmap bitmapImage = new Bitmap(piccaptcha.Image); var ms = new MemoryStream(); Image image = piccaptcha.Image; image.Save(ms, ImageFormat.Png); var bytes = ms.ToArray(); var imageMemoryStream = new MemoryStream(bytes); Image imgFromStream = Image.FromStream(imageMemoryStream); imgFromStream.Save("captcha.jpg", ImageFormat.Jpeg); DebugHelper.VerboseMode = true; var api = new ImageToText { ClientKey = "ef85f1b1baa494a70b68cf1212727dcd", FilePath = "captcha.jpg" }; if (!api.CreateTask()) { DebugHelper.Out("API v2 send failed. " + api.ErrorMessage, DebugHelper.Type.Error); } else if (!api.WaitForResult()) { DebugHelper.Out("Could not solve the captcha.", DebugHelper.Type.Error); } else { DebugHelper.Out("Result: " + api.GetTaskSolution().Text, DebugHelper.Type.Success); captcha = api.GetTaskSolution().Text; txtcaptcha.Text = captcha; } }
public void ConvertHtmlToImage(string html) { try { var htmlToImageConv = new NReco.ImageGenerator.HtmlToImageConverter(); var jpegBytes = htmlToImageConv.GenerateImage(html, ImageFormat.Png.ToString()); MemoryStream memstr = new MemoryStream(jpegBytes); System.Drawing.Image img = System.Drawing.Image.FromStream(memstr, true, true); img.Save(Server.MapPath("~/Images/") + Path.GetRandomFileName() + ".jpeg", ImageFormat.Jpeg); } catch (Exception ex) { throw ex; } }
public static void ConvertHtmlToImage(string htmlCode, string zName, string path) { var htmlToImageConv = new NReco.ImageGenerator.HtmlToImageConverter(); var jpegBytes = htmlToImageConv.GenerateImage(htmlCode, "png"); Image newImage = (Bitmap)((new ImageConverter()).ConvertFrom(jpegBytes)); Rectangle cropRect = new Rectangle(0, 24, 600, 600); Bitmap src = newImage as Bitmap; Bitmap target = new Bitmap(cropRect.Width, cropRect.Height); using (Graphics g = Graphics.FromImage(target)) { g.DrawImage(src, new Rectangle(0, 24, target.Width, target.Height), cropRect, GraphicsUnit.Pixel); } target.Save(Path.Combine(path, zName + "_" + DateTime.Now.ToString("MM-dd") + ".png"), ImageFormat.Png); }
public static void Main(string[] args) { //Console.WriteLine(new Random().Next(0,9999)); Console.WriteLine("*** Begin " + DateTime.Now + " *** "); var url = "http://xiaobao1001.cn/Home/InvitationRoute?p=" + HttpUtility.UrlEncode(string.Format("oldinvitenew?id={0}&iscutpage=true", 12418)); //for (var i = 1; i <= 10; i++) //{ // Console.WriteLine(Guid.NewGuid()); // Console.WriteLine(Guid.NewGuid().ToString("N")); // Console.WriteLine("*** Begin " + DateTime.Now + " 线程:" + i + " *** "); // var i1 = i; // Task.Run(() => { new NRecoPhantomJSTest().Test(i1); }); //} //new NRecoPhantomJSTest().Test(0); //Console.WriteLine("*** End " + DateTime.Now + " *** "); //Console.WriteLine((DateTime.Now.AddSeconds(5) - DateTime.Now).Ticks); //var ticks = (DateTime.Now.AddSeconds(5) - DateTime.Now).Ticks; //var ts = new TimeSpan(ticks); //Console.WriteLine(ts.TotalSeconds); var html = @"<!DOCTYPE html><html><head lang=""zh-CN""><meta charset=""UTF-8""><title>生产图片</title></head><body> <div class=""A4-page-content""><div class=""A4-content-slogan""><div class=""content-slogan-tirle"">圣安东尼跆拳道</div><div class=""content-slogan-tips"">专注致力于跆拳道培训</div></div> <div class=""A4-content-code""><div class=""content-code-box""><img src=""http://greedyint-dev.oss-cn-hangzhou.aliyuncs.com/xbshow/QrCode/20160722180710-a9dbc.jpg"" /></div> <div class=""content-code-tips"">扫码免费试课</div></div><div class=""A4-content-address""><div class=""content-address-item""><h3 class=""item-address-h3"">联系方式</h3> <p>座机:0571-88888888<br/>刘老师 电话:13733333333<br/>微信:admin007</p></div><div class=""content-address-item""><h3 class=""item-address-h3"">地址</h3><p>圣安东尼跆拳道学校宁波分校3号楼201室</p> </div></div></body></html>"; var ms = new MemoryStream(); //var html = String.Format("<body>Hello world: {0}</body>", DateTime.Now); //http://xiaobao1001.cn/Home/InvitationRoute?p=oldinvitenew?id=12418&iscutpage=true var resUrl = string.Format("http://{0}/Home/InvitationRoute?p={1}", "xiaobao1001.cn", HttpUtility.UrlEncode(string.Format("oldinvitenew?id={0}&iscutpage=true", 615))); var htmlToImageConv = new NReco.ImageGenerator.HtmlToImageConverter(); //htmlToImageConv.Height = 1200; //htmlToImageConv.Width = 1080; //htmlToImageConv.ExecutionTimeout = new TimeSpan(DateTime.Now.AddSeconds(10).Ticks); //htmlToImageConv.GenerateImageFromFile("https://detail.tmall.com/item.htm?spm=a220o.1000855.w5001-12072074390.4.4y6Jne&id=530873358617&rn=eb52ae8b524e7c1f0463cfb60c25acd0&abbucket=1&scene=taobao_shop", ImageFormat.Jpeg.ToString(), ms); htmlToImageConv.GenerateImage(html, ImageFormat.Jpeg.ToString(), ms); var bitMap = new Bitmap(ms); var path = @"D:\work\He\CommonTest\PhantomJsConsole\bin\Debug\" + Guid.NewGuid(); bitMap.Save(path + ".jpg"); bitMap.Dispose(); Console.ReadLine(); }
private async void btnIniciarConsulta_Click(object sender, EventArgs e) { do { //foreach (var consul in layout) //{ wb.Document.GetElementById("aceiteTermoUso").SetAttribute("checked", "true"); if (!string.IsNullOrEmpty(layout[totalconsultado].CPFCNPJ)) { txtcpf.Text = layout[totalconsultado].CPFCNPJ; wb.Document.GetElementById("cpfCnpjEmitente").InnerText = txtcpf.Text; } if (!string.IsNullOrEmpty(layout[totalconsultado].CM7D1)) { txtd1.Text = layout[totalconsultado].CM7D1; wb.Document.GetElementById("primeiroCampoCmc7").InnerText = txtd1.Text; } if (!string.IsNullOrEmpty(layout[totalconsultado].CM7D2)) { txtd2.Text = layout[totalconsultado].CM7D2; wb.Document.GetElementById("segundoCampoCmc7").InnerText = txtd2.Text; } if (!string.IsNullOrEmpty(layout[totalconsultado].CM7D3)) { txtd3.Text = layout[totalconsultado].CM7D3; wb.Document.GetElementById("terceiroCampoCmc7").InnerText = txtd3.Text; } if (!string.IsNullOrEmpty(layout[totalconsultado].CPFINTERESSADO)) { txtcpfinteressado.Text = layout[totalconsultado].CPFINTERESSADO; wb.Document.GetElementById("cpfCnpjInteressado").InnerText = txtcpfinteressado.Text; } var text = wb.Document.GetElementById("cipCaptchaImg"); var html = String.Format("{0}{1}{2}", "<table cellpadding=\"0\" cellspacing=\"0\">", text.InnerHtml, "</table>"); var htmlToImageConv = new NReco.ImageGenerator.HtmlToImageConverter(); // htmlToImageConv.Height = 70; htmlToImageConv.Width = 200; var jpegBytes = htmlToImageConv.GenerateImage(html, "Png"); piccaptcha.Image = ByteArrayToImage(jpegBytes); Bitmap bitmapImage = new Bitmap(piccaptcha.Image); var ms = new MemoryStream(); Image image = piccaptcha.Image; image.Save(ms, ImageFormat.Png); var bytes = ms.ToArray(); var imageMemoryStream = new MemoryStream(bytes); Image imgFromStream = Image.FromStream(imageMemoryStream); imgFromStream.Save("captcha.jpg", ImageFormat.Jpeg); DebugHelper.VerboseMode = true; var api = new ImageToText { ClientKey = "ef85f1b1baa494a70b68cf1212727dcd", FilePath = "captcha.jpg" }; if (!api.CreateTask()) { DebugHelper.Out("API v2 send failed. " + api.ErrorMessage, DebugHelper.Type.Error); } else if (!api.WaitForResult()) { DebugHelper.Out("Could not solve the captcha.", DebugHelper.Type.Error); } else { DebugHelper.Out("Result: " + api.GetTaskSolution().Text, DebugHelper.Type.Success); captcha = api.GetTaskSolution().Text; txtcaptcha.Text = captcha; } if (!string.IsNullOrEmpty(captcha)) { wb.Document.GetElementById("captcha").InnerText = captcha; } //status.Text = "Aguardar 3 segundos"; //Thread.Sleep(3000); wb.Document.GetElementById("btEnviar").InvokeMember("click"); //Thread.Sleep(5000); var item = wb.Document.GetElementById("errors"); if (item.InnerHtml != null) { if ((item.InnerText.Contains(@"Código da Imagem: Caracteres do captcha não foram preenchidos corretamente ou o tempo máximo para preenchimento foi ultrapassado")) || (item.InnerText.Contains(@"Código da Imagem é um campo obrigatório"))) { //MessageBox.Show(@"Código da Imagem: Caracteres do captcha não foram preenchidos corretamente ou o tempo máximo para preenchimento foi ultrapassado"); status.Text = @"Código da Imagem: Caracteres do captcha não foram preenchidos corretamente ou o tempo máximo para preenchimento foi ultrapassado"; //goto repete; } } atualizaConsultado(); } while (totalconsultado < totalConsulta); //} }
private async void btnConsultar_Click(object sender, EventArgs e) { wb.Document.GetElementById("aceiteTermoUso").SetAttribute("checked", "true"); if (!string.IsNullOrEmpty(txtcpf.Text)) { wb.Document.GetElementById("cpfCnpjEmitente").InnerText = txtcpf.Text.Trim(); } if (!string.IsNullOrEmpty(txtd1.Text)) { wb.Document.GetElementById("primeiroCampoCmc7").InnerText = txtd1.Text.Trim(); } if (!string.IsNullOrEmpty(txtd2.Text)) { wb.Document.GetElementById("segundoCampoCmc7").InnerText = txtd2.Text.Trim(); } if (!string.IsNullOrEmpty(txtd3.Text)) { wb.Document.GetElementById("terceiroCampoCmc7").InnerText = txtd3.Text.Trim(); } if (!string.IsNullOrEmpty(txtcpfinteressado.Text)) { wb.Document.GetElementById("cpfCnpjInteressado").InnerText = txtcpfinteressado.Text.Trim(); } var text = wb.Document.GetElementById("cipCaptchaImg"); var html = String.Format("{0}{1}{2}", "<table cellpadding=\"0\" cellspacing=\"0\">", text.InnerHtml, "</table>"); var htmlToImageConv = new NReco.ImageGenerator.HtmlToImageConverter(); // htmlToImageConv.Height = 70; htmlToImageConv.Width = 200; var jpegBytes = htmlToImageConv.GenerateImage(html, "Png"); piccaptcha.Image = ByteArrayToImage(jpegBytes); Bitmap bitmapImage = new Bitmap(piccaptcha.Image); var ms = new MemoryStream(); Image image = piccaptcha.Image; image.Save(ms, ImageFormat.Png); var bytes = ms.ToArray(); var imageMemoryStream = new MemoryStream(bytes); Image imgFromStream = Image.FromStream(imageMemoryStream); imgFromStream.Save("captcha.jpg", ImageFormat.Jpeg); DebugHelper.VerboseMode = true; var api = new ImageToText { ClientKey = "ef85f1b1baa494a70b68cf1212727dcd", FilePath = "captcha.jpg" }; if (!api.CreateTask()) { DebugHelper.Out("API v2 send failed. " + api.ErrorMessage, DebugHelper.Type.Error); } else if (!api.WaitForResult()) { DebugHelper.Out("Could not solve the captcha.", DebugHelper.Type.Error); } else { DebugHelper.Out("Result: " + api.GetTaskSolution().Text, DebugHelper.Type.Success); wb.Document.GetElementById("captcha").InnerText = api.GetTaskSolution().Text; txtcaptcha.Text = api.GetTaskSolution().Text; } if (!string.IsNullOrEmpty(txtcaptcha.Text)) { wb.Document.GetElementById("captcha").InnerText = txtcaptcha.Text.Trim(); } wb.Document.GetElementById("btEnviar").InvokeMember("click"); }
public override void Persist() { // Current Data var _currData = (LumItemsCOC)_viewLine.Cache.Current; // Create Upload Image Graph UploadFileMaintenance upload = PXGraph.CreateInstance <UploadFileMaintenance>(); // Html to Image and Upload to Server Action <string, string> htmlToImage = (_html, _fileName) => { HtmlDocument _doc = new HtmlDocument(); _doc.LoadHtml(_html); if (string.IsNullOrEmpty(_doc.DocumentNode.InnerText)) { return; } var htmlNode = _doc.DocumentNode.SelectSingleNode("//html"); HtmlNode newNode = HtmlNode.CreateNode(@"<meta http-equiv=""content-type"" content=""text/html; charset=utf-8"" />"); htmlNode.InsertBefore(newNode, htmlNode.ChildNodes[0]); var htmlToImageConv = new NReco.ImageGenerator.HtmlToImageConverter(); var jpegBytes = htmlToImageConv.GenerateImage(_doc.DocumentNode.OuterHtml, NReco.ImageGenerator.ImageFormat.Jpeg); FileInfo file = new FileInfo($"{_fileName}.jpeg", null, jpegBytes); upload.SaveFile(file, FileExistsAction.CreateVersion); //To Attach file to Entity PXNoteAttribute.SetFileNotes(_viewLine.Cache, _viewLine.Cache.Current, file.UID.Value); }; // Process All Tab #region MaterialProductDesc if (!CheckInnerTextIsEmpty(_currData.MaterialProductDesc)) { htmlToImage(_currData.MaterialProductDesc, $"COC_{_currData.InventoryID}_MaterialProductDesc"); } else { _viewLine.Cache.SetValue <LumItemsCOC.materialProductDesc>(_viewLine.Cache.Current, null); } #endregion #region MaterialProductDesc2 if (!CheckInnerTextIsEmpty(_currData.MaterialProductDesc2)) { htmlToImage(_currData.MaterialProductDesc2, $"COC_{_currData.InventoryID}_MaterialProductDesc2"); } else { _viewLine.Cache.SetValue <LumItemsCOC.materialProductDesc2>(_viewLine.Cache.Current, null); } #endregion ; #region COCProductDesc if (!CheckInnerTextIsEmpty(_currData.COCProductDesc)) { htmlToImage(_currData.COCProductDesc, $"COC_{_currData.InventoryID}_COCProductDesc"); } else { _viewLine.Cache.SetValue <LumItemsCOC.cOCProductDesc>(_viewLine.Cache.Current, null); } #endregion #region TESTProductDesc if (!CheckInnerTextIsEmpty(_currData.TESTProductDesc)) { htmlToImage(_currData.TESTProductDesc, $"COC_{_currData.InventoryID}_TESTProductDesc"); } else { _viewLine.Cache.SetValue <LumItemsCOC.tESTProductDesc>(_viewLine.Cache.Current, null); } #endregion #region REROHSProductDesc if (!CheckInnerTextIsEmpty(_currData.REROHSProductDesc)) { htmlToImage(_currData.REROHSProductDesc, $"COC_{_currData.InventoryID}_REROHSProductDesc"); } else { _viewLine.Cache.SetValue <LumItemsCOC.rEROHSProductDesc>(_viewLine.Cache.Current, null); } #endregion #region REACHProductDesc if (!CheckInnerTextIsEmpty(_currData.REACHProductDesc)) { htmlToImage(_currData.REACHProductDesc, $"COC_{_currData.InventoryID}_REACHProductDesc"); } else { _viewLine.Cache.SetValue <LumItemsCOC.rEACHProductDesc>(_viewLine.Cache.Current, null); } #endregion #region REACHProductDesc2 if (!CheckInnerTextIsEmpty(_currData.REACHProductDesc2)) { htmlToImage(_currData.REACHProductDesc2, $"COC_{_currData.InventoryID}_REACHProductDesc2"); } else { _viewLine.Cache.SetValue <LumItemsCOC.rEACHProductDesc2>(_viewLine.Cache.Current, null); } #endregion #region Compliantproductdesc if (!CheckInnerTextIsEmpty(_currData.Compliantproductdesc)) { htmlToImage(_currData.Compliantproductdesc, $"COC_{_currData.InventoryID}_Compliantproductdesc"); } else { _viewLine.Cache.SetValue <LumItemsCOC.compliantproductdesc>(_viewLine.Cache.Current, null); } #endregion #region QCProductDesc if (!CheckInnerTextIsEmpty(_currData.QCProductDesc)) { htmlToImage(_currData.QCProductDesc, $"COC_{_currData.InventoryID}_QCProductDesc"); } else { _viewLine.Cache.SetValue <LumItemsCOC.qCProductDesc>(_viewLine.Cache.Current, null); } #endregion base.Persist(); }
private void saveToImage(string html) { var htmlToImageConv = new NReco.ImageGenerator.HtmlToImageConverter(); var jpegBytes = htmlToImageConv.GenerateImage(html, ImageFormat.Png.ToString()); File.WriteAllBytes("c:\\test\\image.png", jpegBytes); }