private void IndicateSectionStart(InlineImage icon, string indicator) { sectionIndicator = indicator; Log.Message(sectionIndicator + HtmlLogSystem.HTML__LINE_BREAK + Log.HTML__SECTIONSTART, LoggingLevel.Critical, icon); }
private string NormalLine(InlineImage icon, string message) { StringBuilder result = new StringBuilder(HTML__ROW_START + HTML__TABLE_BEGIN_DATA_CELL); if (SHOW_TIME_STAMPS) { result.Append(HTML__TABLE_BEGIN_DATA_CELL); result.Append(HtmlTimeStamp); result.Append(HTML__TABLE_END_DATA_CELL); } result.Append(HTML__TABLE_BEGIN_DATA_CELL); if (icon == null) { result.Append(HTML__SPACE); } else { result.Append(icon); } result.Append(HTML__TABLE_DIVISION); // DEBUG // List<string> messageSorted = Foundation.sortMarkupTags(message); // For loop below is to properly handle embedded HTML tags. foreach (string thisString in message.SortMarkupTags()) { if (thisString.StartsWith("<")) { if (thisString.Length > 60) { result.Append(Symbols.CarriageReturnLineFeed + thisString + Symbols.CarriageReturnLineFeed); } else { result.Append(thisString); } } else { //use regex // FIXED result.Append(Regex.Replace(thisString.Justify(HTML_LOG_JUSTIFY_SIZE), Symbols.CarriageReturnLineFeed, "<br>" + Symbols.CarriageReturnLineFeed)); } } result.Append(HTML__TABLE_END_DATA_CELL); result.Append(HTML__ROW_END); return(result.ToString()); }
public void Run(ICommandInteraction writer, DecimalIntegerToken width, DecimalIntegerToken height) { if (width.Value <= 0 || height.Value <= 0) { throw new RecoverableException("Width and height must be positive values"); } var image = new Bitmap((int)width.Value, (int)height.Value); using (var graphics = Graphics.FromImage(image)) { var imageSize = new Rectangle(0, 0, (int)width.Value, (int)height.Value); graphics.FillRectangle(Brushes.White, imageSize); } writer.WriteRaw(InlineImage.Encode(image)); }
public void Run(ICommandInteraction writer, PathToken pathToImage) { if (!File.Exists(pathToImage.Value)) { writer.WriteError($"No such file {pathToImage.Value}"); return; } using (var file = new FileStream(pathToImage.Value, FileMode.Open)) { if (!CheckFormat(file)) { writer.WriteError("Bad image format. Supported formats: jpeg, png"); return; } writer.WriteRaw(InlineImage.Encode(file)); } }
public void Run(ICommandInteraction writer, DecimalIntegerToken widthToken, DecimalIntegerToken heightToken) { // This method creates white rectangle, intended for testing purposes. var width = (int)widthToken.Value; var height = (int)heightToken.Value; if (width <= 0 || height <= 0) { throw new RecoverableException("Width and height must be positive values"); } var bytes = new byte[width * height * RawImageData.PixelFormat.GetColorDepth()]; for (var i = 0; i < bytes.Length; ++i) { bytes[i] = (byte)0xFF; } var image = new RawImageData(bytes, width, height); writer.WriteRaw(InlineImage.Encode(image.ToPng())); }
private void createReport() { var someText = "This is some text, just to fill some words between structure elements"; var figure = new Figure("Figure", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "figure.png")); var inlineImage = new InlineImage(figure.FullPath); _objectsToReport.Add(new Part("This is the first part using simple strings")); _objectsToReport.Add(someText); _objectsToReport.Add(new Chapter("Simple Chapter")); _objectsToReport.Add(someText); _objectsToReport.Add(figure); _objectsToReport.Add(new Section("Simple Section")); _objectsToReport.Add(someText); _objectsToReport.Add(new SubSection("Simple SubSection")); _objectsToReport.Add(someText); _objectsToReport.Add(new SubSubSection("Simple SubSubSection")); _objectsToReport.Add(someText); _objectsToReport.Add(new Paragraph("Simple Paragraph")); _objectsToReport.Add(someText); _objectsToReport.Add(new SubParagraph("Simple SubParagraph")); _objectsToReport.Add(someText); _objectsToReport.Add(new Part("This is the first part using text objects with inline image", new Text("This is the first part using text objects with inline image {0}.", inlineImage))); _objectsToReport.Add(someText); _objectsToReport.Add(new Chapter("Text Chapter", new Text("Text Chapter with {0} image.", inlineImage))); _objectsToReport.Add(someText); _objectsToReport.Add(figure); _objectsToReport.Add(new Section("Text Section", new Text("Text Section with {0} image.", inlineImage))); _objectsToReport.Add(someText); _objectsToReport.Add(new SubSection("Text SubSection", new Text("Text SubSection with {0} image.", inlineImage))); _objectsToReport.Add(someText); _objectsToReport.Add(new SubSubSection("Text SubSubSection", new Text("Text SubSubSection with {0} image.", inlineImage))); _objectsToReport.Add(someText); _objectsToReport.Add(new Paragraph("Text Paragraph", new Text("Text Paragraph with {0} image.", inlineImage))); _objectsToReport.Add(someText); _objectsToReport.Add(new SubParagraph("Text SubParagraph", new Text("Text SubParagraph with {0} image.", inlineImage))); _objectsToReport.Add(someText); }
public void Run(ICommandInteraction writer, PathToken pathToImage) { if (!File.Exists(pathToImage.Value)) { writer.WriteError($"No such file {pathToImage.Value}"); return; } try { var image = new Bitmap(pathToImage.Value); if (!ImageFormat.Jpeg.Equals(image.RawFormat) && !ImageFormat.Png.Equals(image.RawFormat)) { writer.WriteError("Bad image format. Supported formats: jpeg, png"); return; } writer.WriteRaw(InlineImage.Encode(image)); } catch (Exception e) { writer.WriteError($"There was an error when loading the image: {(e.Message)}"); } }
public virtual IDictionary <string, object> ToDictionary(InlineImage inlineImage) { return(ToDictionary(inlineImage as File)); }
/** * <summary>Scans a content level looking for images.</summary> */ /* * NOTE: Page contents are represented by a sequence of content objects, * possibly nested into multiple levels. */ private void Scan( ContentScanner level, Page page ) { if (level == null) { return; } while (level.MoveNext()) { ContentObject current = level.Current; if (current is ContainerObject) { // Scan the inner level! Scan( level.ChildLevel, page ); } else { ContentScanner.GraphicsObjectWrapper objectWrapper = level.CurrentWrapper; if (objectWrapper == null) { continue; } /* * NOTE: Images can be represented on a page either as * external objects (XObject) or inline objects. */ SizeF?imageSize = null; // Image native size. if (objectWrapper is ContentScanner.XObjectWrapper) { ContentScanner.XObjectWrapper xObjectWrapper = (ContentScanner.XObjectWrapper)objectWrapper; xObjects::XObject xObject = xObjectWrapper.XObject; // Is the external object an image? if (xObject is xObjects::ImageXObject) { Console.Write( "External Image '" + xObjectWrapper.Name + "' (" + xObject.BaseObject + ")" // Image key and indirect reference. ); imageSize = xObject.Size; // Image native size. } } else if (objectWrapper is ContentScanner.InlineImageWrapper) { Console.Write("Inline Image"); InlineImage inlineImage = ((ContentScanner.InlineImageWrapper)objectWrapper).InlineImage; imageSize = inlineImage.Size; // Image native size. } if (imageSize.HasValue) { RectangleF box = objectWrapper.Box.Value; // Image position (location and size) on the page. Console.WriteLine( " on page " + page.Number + " (" + page.BaseObject + ")" // Page index and indirect reference. ); Console.WriteLine(" Coordinates:"); Console.WriteLine(" x: " + Math.Round(box.X)); Console.WriteLine(" y: " + Math.Round(box.Y)); Console.WriteLine(" width: " + Math.Round(box.Width) + " (native: " + Math.Round(imageSize.Value.Width) + ")"); Console.WriteLine(" height: " + Math.Round(box.Height) + " (native: " + Math.Round(imageSize.Value.Height) + ")"); } } } }
public void ShowException(Exception loggedException, LoggingLevel requestedLevel = LoggingLevel.Critical, InlineImage icon = null) { string currentLine = default(string); try { StringBuilder result = new StringBuilder(HTML__EXCEPTION_TABLE_START); string[] lines = Foundation.DepictException(loggedException).Replace("\r", "").Split('\n'); for (int cursor = 0; cursor < lines.Length; cursor++) { currentLine = lines[cursor].Trim(); if (currentLine != null) { if (currentLine.Trim().Length > 0) { result.Append(HTML__ROW_START); if (currentLine.StartsWith("at")) { string[] parts = currentLine.Trim().Replace("at ", "").Replace(":line", "|").Replace(".cs:", ".cs|").Replace(" in ", "|").Split('|'); if (parts.Length > 2) { result.Append(HTML__TABLE_BEGIN_DATA_CELL); result.Append("<small>"); result.Append(HTML__SPACE); result.Append(HTML__SPACE); result.Append(parts[0].Trim()); result.Append("()</small>"); result.Append(HTML__TABLE_END_DATA_CELL); result.Append(HTML__TABLE_BEGIN_DATA_CELL); result.Append(HTML__SPACE); result.Append(HTML__TABLE_END_DATA_CELL); result.Append(HTML__TABLE_BEGIN_DATA_CELL); result.Append("<b>"); result.Append(parts[1].Trim()); result.Append("</b>"); result.Append(HTML__TABLE_END_DATA_CELL); result.Append(HTML__TABLE_BEGIN_DATA_CELL); result.Append(HTML__SPACE); result.Append("<small><i>at line</i></small>"); result.Append(HTML__SPACE); result.Append(HTML__TABLE_END_DATA_CELL); result.Append(HTML__TABLE_BEGIN_RIGHT_ALIGN_DATA_CELL); result.Append("<b>"); result.Append(parts[2].Trim()); result.Append(HTML__SPACE); result.Append(HTML__SPACE); result.Append("</b>"); } else if (parts.Length > 1) { result.Append(HTML__TABLE_BEGIN_DATA_CELL); result.Append("<small>"); result.Append(HTML__SPACE); result.Append(HTML__SPACE); result.Append(parts[0].Trim()); result.Append("()</small>"); result.Append(HTML__TABLE_END_DATA_CELL); result.Append(HTML__TABLE_BEGIN_DATA_CELL); result.Append(HTML__SPACE); result.Append(HTML__TABLE_END_DATA_CELL); result.Append(HTML__TABLE_BEGIN_WIDE_DATA_CELL); result.Append("<b>"); result.Append(parts[1].Trim()); result.Append("</b>"); result.Append(HTML__TABLE_END_DATA_CELL); } else { result.Append(HTML__TABLE_BEGIN_WIDE_DATA_CELL); result.Append(singleFailureLine(currentLine.Replace("at ", HTML__SPACE + HTML__SPACE))); } } else if (currentLine.ToLower().StartsWith("inner")) { string[] parts = currentLine.Replace("Inner ", "").Split(':'); if (parts.Length > 1) { result.Append(HTML__TABLE_BEGIN_WIDE_DATA_CELL); result.Append(HTML__SPACE); result.Append(HTML__TABLE_END_DATA_CELL); result.Append(HTML__ROW_END); result.Append(HTML__ROW_START); result.Append(HTML__TABLE_BEGIN_WIDE_DATA_CELL); result.Append("<small>...caused by...</small><center><b>"); result.Append(parts[0].Trim()); result.Append("</b>"); if (parts.Length > 1) { result.Append("<br><small><i>"); result.Append(parts[1].Trim()); result.Append("</i></small>"); } result.Append("</center><br>"); } else { result.Append(HTML__TABLE_BEGIN_WIDE_DATA_CELL); result.Append(singleFailureLine(currentLine)); } } else if (currentLine.StartsWith("...")) { result.Append(HTML__TABLE_BEGIN_WIDE_DATA_CELL); result.Append("<small><i>("); result.Append(currentLine.Replace("... ", "")); result.Append(")</i></small>"); } else { result.Append(HTML__TABLE_BEGIN_WIDE_DATA_CELL); string[] parts = currentLine.Split(':'); if (currentLine.Contains(loggedException.GetType().Name)) { result.Append("<center><h3>"); result.Append(parts[0]); result.Append("</h3>"); if (parts.Length > 1) { result.Append("<small><i>"); result.Append(parts[1].Trim()); result.Append("</i></small>"); } result.Append("</center><br>"); } else { //use regex // FIXED result.Append(singleFailureLine(Regex.Replace(currentLine, Symbols.CarriageReturnLineFeed, " "))); } } result.Append(HTML__TABLE_END_DATA_CELL); result.Append(HTML__ROW_END); } } } result.Append(HTML__TABLE_BEGIN_WIDE_DATA_CELL); result.Append("<small>"); result.Append(HTML__SPACE); result.Append("</small>"); result.Append(HTML__TABLE_END_DATA_CELL); result.Append(HTML__EXCEPTION_TABLE_END); Message(result.ToString(), requestedLevel, icon); Flush(); } catch (Exception thisException) { Console.Out.WriteLine(); Console.Out.WriteLine("Programming Error in showFailure():"); Console.Out.WriteLine(Foundation.DepictException(thisException)); Console.Out.WriteLine(); Console.Out.WriteLine("Current line was:"); Console.Out.WriteLine(currentLine); Console.Out.WriteLine(); Console.Out.WriteLine("Failure to log the following exception:"); Console.Out.WriteLine(Foundation.DepictException(loggedException)); Console.Out.WriteLine(); } }
public void Message(string message, LoggingLevel requestedLevel = LoggingLevel.Standard, InlineImage icon = null) { WriteLine(NormalLine(icon, message), requestedLevel); }
/// <summary> /// inheritedDoc /// </summary> public bool PrepareImages(MailMessage message) { if (!message.IsBodyHtml) { return(false); } var images = new List <InlineImage>(); // regular expression to extract Urls for img tags var imgRegex = new Regex(@"(?<=img\s+[\w\s\""\'\=\:\;\\\/\.]*src\=[\x27\x22])(?<Url>[^\x27\x22]*)(?=[\x27\x22])"); var imgIndex = 0; // replace Urls in <img> tags var newBody = imgRegex.Replace(message.Body, m => { var res = m.Value; var embeddedImgRegex = new Regex(@"data:(?<contentType>image/[\w]+);base64,"); var embeddedMatches = embeddedImgRegex.Match(res); if (embeddedMatches.Success) { var contentType = embeddedMatches.Groups["contentType"].Value; var imageBase64 = embeddedImgRegex.Replace(res, ""); var imageBytes = Convert.FromBase64String(imageBase64); var tmp = Path.ChangeExtension(Path.GetTempFileName(), MimeTypeMap.GetExtension(contentType)); using (var imageFile = new FileStream(tmp, FileMode.Create)) { imageFile.Write(imageBytes, 0, imageBytes.Length); imageFile.Flush(); } var image = new InlineImage { FilePath = tmp, Name = $"image{imgIndex++}", ContentType = contentType }; images.Add(image); res = "cid:" + image.Name; } return(res); }); message.Body = newBody; var htmlView = AlternateView.CreateAlternateViewFromString(newBody, null, "text/html"); foreach (var image in images) { var res = new LinkedResource(image.FilePath, image.ContentType) { ContentId = image.Name }; htmlView.LinkedResources.Add(res); } message.AlternateViews.Add(htmlView); message.IsBodyHtml = true; return(true); }