public static byte[] Screenshot(Chrome instance, string url, int cropWidth = 1000, int cropHeight = 1000) { lock (lockObj) { try { Chrome.logger.Debug($"{instance.Name}: Screenshot GoToUrl {url}"); instance.WebDriver().GoToUrl(url).Wait(); System.Threading.Thread.Sleep(50); Chrome.logger.Debug($"{instance.Name}: Screenshot GetScreenshot {url}"); var screenshot = instance.WebDriver().GetScreenshot().Result; Chrome.logger.Debug($"{instance.Name}: Screenshot Cropping {url}"); using (Devmasters.Imaging.InMemoryImage img = new Devmasters.Imaging.InMemoryImage(screenshot.AsByteArray)) { img.Crop(new System.Drawing.Rectangle(0, 0, cropWidth, cropHeight)); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { img.SaveAsPNG(ms); return(ms.ToArray()); } } } catch (Exception e) { Chrome.logger.Error($"{instance.Name}: Screenshot Error {url}", e); instance.RecreatedChrome(); return(null); } finally { Chrome.logger.Debug($"{instance.Name}: Screenshot DONE {url}"); } } }
public ActionResult ChangePhoto(string id, FormCollection form, HttpPostedFileBase file1) { try { if (string.IsNullOrEmpty(id)) { return(RedirectToAction(nameof(OsobyController.Index), "Osoby")); } var o = HlidacStatu.Lib.Data.Osoby.GetByNameId.Get(id); if (o == null) { return(NotFound("/", "Pokračovat na titulní straně")); } ViewBag.Osoba = o; if (form["phase"] == "start") //upload { byte[] data = null; var path = HlidacStatu.Lib.Init.OsobaFotky.GetFullPath(o, "original.uploaded.jpg"); var pathTxt = HlidacStatu.Lib.Init.OsobaFotky.GetFullPath(o, "source.txt"); var source = form["source"] ?? ""; string[] facesFiles = new string[] { }; if (!string.IsNullOrEmpty(form["url"])) { try { data = new System.Net.WebClient().DownloadData(form["url"]); source = form["url"]; } catch (Exception) { data = null; } } else if (file1.ContentLength > 0) { using (var binaryReader = new BinaryReader(file1.InputStream)) { data = binaryReader.ReadBytes(file1.ContentLength); } } try { facesFiles = HlidacStatu.Lib.RenderTools.DetectAndParseFacesIntoFiles(data, 150, 40).ToArray(); if (data != null && facesFiles.Length > 0) { System.IO.File.WriteAllText(pathTxt, source); System.IO.File.WriteAllBytes(path, data); } } catch (Exception e) { HlidacStatu.Util.Consts.Logger.Error("PhotoChange processing", e); } ViewBag.Osoba = o; ViewBag.Phase = "choose"; ViewBag.Email = form["email"] ?? ""; return(View("ChangePhoto_Choose", facesFiles)); } //upload else if (form["phase"] == "choose") //upload { string fn = form["fn"]; if (!string.IsNullOrEmpty(fn) && fn.Contains(System.IO.Path.GetTempPath())) { if (System.IO.File.Exists(fn)) { //C:\Windows\TEMP\tmp3EB0.tmp.0.faces.jpg var rootfn = Devmasters.RegexUtil.GetRegexGroupValue(fn, @"(?<tempfn>.*)\.\d{1,2}\.faces\.jpg$", "tempfn"); var target = HlidacStatu.Lib.Init.OsobaFotky.GetFullPath(o, "small.uploaded.jpg"); try { using (Devmasters.Imaging.InMemoryImage imi = new Devmasters.Imaging.InMemoryImage(fn)) { Devmasters.IO.IOTools.DeleteFile(fn); if (this.User?.IsInRole("Admin") == true) { //Devmasters.IO.IOTools.MoveFile(fn, HlidacStatu.Lib.Init.OsobaFotky.GetFullPath(o, "small.jpg")); imi.Resize(new System.Drawing.Size(300, 300), true, Devmasters.Imaging.InMemoryImage.InterpolationsQuality.High, true); imi.SaveAsJPEG(HlidacStatu.Lib.Init.OsobaFotky.GetFullPath(o, "small.jpg"), 80); return(Redirect("/Osoba/" + o.NameId)); } else { imi.SaveAsJPEG(target, 80); //Devmasters.IO.IOTools.MoveFile(fn, HlidacStatu.Lib.Init.OsobaFotky.GetFullPath(o, "small.review.jpg")); //Devmasters.IO.IOTools.MoveFile(HlidacStatu.Lib.Init.OsobaFotky.GetFullPath(o, "uploaded.jpg"), HlidacStatu.Lib.Init.OsobaFotky.GetFullPath(o, "original.uploaded.jpg")); using (HlidacStatu.Lib.Data.DbEntities db = new DbEntities()) { var r = new Review() { created = DateTime.Now, createdBy = form["email"] ?? "", itemType = "osobaPhoto", newValue = Newtonsoft.Json.JsonConvert.SerializeObject(new { nameId = o.NameId, file = target }), }; db.Review.Add(r); using (System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient()) { smtp.Send("*****@*****.**", "*****@*****.**", "Photo review", Newtonsoft.Json.JsonConvert.SerializeObject(r, Newtonsoft.Json.Formatting.Indented) ); } db.SaveChanges(); } return(View("ChangePhoto_finish", o)); } } } finally { try { foreach (var f in System.IO.Directory.EnumerateFiles(System.IO.Path.GetDirectoryName(rootfn), System.IO.Path.GetFileName(rootfn) + ".*")) { Devmasters.IO.IOTools.DeleteFile(f); } } catch { } } } } } } catch (Exception e) { HlidacStatu.Util.Consts.Logger.Error("PhotoChange", e); return(View()); } return(View()); }