예제 #1
0
        public async Task <bool> SaveSnapshot(Stream outputFilestream, int thumbnailSize = -1)
        {
            ClearComponent();

            if (_thumbnailData != null)
            {
                _thumbnailData.Dispose();
                _thumbnailData = null;
            }

            _thumbnailSize      = thumbnailSize;
            _saveSurface        = true;
            thumbnailCompletion = new TaskCompletionSource <bool>();

            _canvas.InvokeInvalidate();

            // wait for the canvas to draw itself.  thumbnail bitmap needs to be set during the draw portion
            await thumbnailCompletion.Task;

            bool result = true;

            try
            {
                _thumbnailData.SaveTo(outputFilestream);
                _thumbnailData.Dispose();
                _thumbnailData = null;
            }
            catch (Exception)
            {
                result = false;
            }
            return(result);
        }
예제 #2
0
 public void Dispose()
 {
     Font?.Dispose();
     Font = null;
     FontStream?.Dispose();
     FontStream = null;
     FontSource?.Dispose();
     FontSource = null;
     ImageBitmap?.Dispose();
     ImageBitmap = null;
     ImageData?.Dispose();
     ImageData = null;
 }
예제 #3
0
        public async Task Invoke(HttpContext context)
        {
            PathString path = context.Request.Path;

            // hand to next middleware if we are not dealing with an image
            if (context.Request.Query.Count == 0 || !IsImagePath(path))
            {
                await _next.Invoke(context);

                return;
            }

            // hand to next middleware if we are dealing with an image but it doesn't have any usable resize querystring params
            ResizeParams resizeParams = GetResizeParams(path, context.Request.Query);

            if (!resizeParams.hasParams || (resizeParams.w == 0 && resizeParams.h == 0))
            {
                await _next.Invoke(context);

                return;
            }

            // get the image location on disk
            string imagePath = Path.Combine(
                _env.WebRootPath,
                path.Value.Replace('/', Path.DirectorySeparatorChar).TrimStart(Path.DirectorySeparatorChar));

            // check file lastwrite
            DateTime lastWriteTimeUtc = File.GetLastWriteTimeUtc(imagePath);

            if (lastWriteTimeUtc.Year == 1601) // file doesn't exist, pass to next middleware
            {
                await _next.Invoke(context);

                return;
            }

            SKData imageData = GetImageData(imagePath, resizeParams, lastWriteTimeUtc);

            // write to stream
            context.Response.ContentType   = resizeParams.format == "png" ? "image/png" : "image/jpeg";
            context.Response.ContentLength = imageData.Size;
            await context.Response.Body.WriteAsync(imageData.ToArray(), 0, (int)imageData.Size);

            // cleanup
            imageData.Dispose();
        }
예제 #4
0
        public void DataDisposedReturnsInvalidStream()
        {
            // create data
            var data = new SKData(OddData);

            // get the stream
            var stream = data.AsStream();

            // nuke the data
            data.Dispose();
            Assert.AreEqual(IntPtr.Zero, data.Handle);

            // read the stream
            var buffer = new byte[OddData.Length];

            stream.Read(buffer, 0, buffer.Length);

            // since the data was nuked, they will differ
            CollectionAssert.AreNotEqual(OddData, buffer);
        }
        public async Task Invoke(HttpContext context)
        {
            if (context.Request.Query.Count == 0 || !context.Request.Path.IsImagePath())
            {
                await _next.Invoke(context);

                return;
            }

            var queryParams = GetQueryParams(context.Request.Path, context.Request.Query);

            if (!queryParams.hasParams)
            {
                await _next.Invoke(context);

                return;
            }

            string path = context.Request.Path;

            var imagePath = Path.Combine(_environment.WebRootPath,
                                         path.Replace('/', Path.DirectorySeparatorChar).TrimStart(Path.DirectorySeparatorChar));

            SKBitmap sKBitmap = LoadBitmap(imagePath);

            SKData sKData = ImageProcess(sKBitmap, queryParams);

            context.Response.ContentType = queryParams.format == "png" ? "image/png" : "image/jpeg";

            context.Response.ContentLength = sKData.Size;

            await context.Response.Body.WriteAsync(sKData.ToArray(), 0, (int)sKData.Size);

            sKBitmap.Dispose();
            sKData.Dispose();
        }
예제 #6
0
        /// <summary>
        /// Generates HTML from Script output.
        /// </summary>
        /// <param name="Result">Script output.</param>
        /// <param name="Output">HTML output.</param>
        /// <param name="AloneInParagraph">If the script output is to be presented alone in a paragraph.</param>
        /// <param name="Variables">Current variables.</param>
        public static void GenerateHTML(object Result, StringBuilder Output, bool AloneInParagraph, Variables Variables)
        {
            if (Result is Graph G)
            {
                using (SKImage Bmp = G.CreateBitmap(Variables, out GraphSettings GraphSettings))
                {
                    SKData Data = Bmp.Encode(SKEncodedImageFormat.Png, 100);
                    byte[] Bin  = Data.ToArray();

                    if (AloneInParagraph)
                    {
                        Output.Append("<figure>");
                    }

                    Output.Append("<img border=\"2\" width=\"");
                    Output.Append(GraphSettings.Width.ToString());
                    Output.Append("\" height=\"");
                    Output.Append(GraphSettings.Height.ToString());
                    Output.Append("\" src=\"data:image/png;base64,");
                    Output.Append(Convert.ToBase64String(Bin, 0, Bin.Length));
                    Output.Append("\" />");

                    if (AloneInParagraph)
                    {
                        Output.Append("</figure>");
                    }

                    Data.Dispose();
                }
            }
            else if (Result is SKImage Img)
            {
                using (SKData Data = Img.Encode(SKEncodedImageFormat.Png, 100))
                {
                    byte[] Bin = Data.ToArray();

                    if (AloneInParagraph)
                    {
                        Output.Append("<figure>");
                    }

                    Output.Append("<img border=\"2\" width=\"");
                    Output.Append(Img.Width.ToString());
                    Output.Append("\" height=\"");
                    Output.Append(Img.Height.ToString());
                    Output.Append("\" src=\"data:image/png;base64,");
                    Output.Append(Convert.ToBase64String(Bin, 0, Bin.Length));
                    Output.Append("\" />");

                    if (AloneInParagraph)
                    {
                        Output.Append("</figure>");
                    }
                }
            }
            else if (Result is Exception ex)
            {
                ex = Log.UnnestException(ex);

                if (ex is AggregateException ex2)
                {
                    foreach (Exception ex3 in ex2.InnerExceptions)
                    {
                        Output.Append("<p><font style=\"color:red\">");
                        Output.Append(XML.HtmlValueEncode(ex3.Message));
                        Output.AppendLine("</font></p>");
                    }
                }
                else
                {
                    if (AloneInParagraph)
                    {
                        Output.Append("<p>");
                    }

                    Output.Append("<font style=\"color:red\">");
                    Output.Append(XML.HtmlValueEncode(ex.Message));
                    Output.Append("</font>");

                    if (AloneInParagraph)
                    {
                        Output.Append("</p>");
                    }
                }
            }
            else if (Result is ObjectMatrix M && M.ColumnNames != null)
            {
                Output.Append("<table><thead><tr>");

                foreach (string s2 in M.ColumnNames)
                {
                    Output.Append("<th>");
                    Output.Append(FormatText(XML.HtmlValueEncode(s2)));
                    Output.Append("</th>");
                }

                Output.Append("</tr></thead><tbody>");

                int x, y;

                for (y = 0; y < M.Rows; y++)
                {
                    Output.Append("<tr>");

                    for (x = 0; x < M.Columns; x++)
                    {
                        Output.Append("<td>");

                        object Item = M.GetElement(x, y).AssociatedObjectValue;
                        if (Item != null)
                        {
                            if (Item is string s2)
                            {
                                Output.Append(FormatText(XML.HtmlValueEncode(s2)));
                            }
                            else
                            {
                                Output.Append(FormatText(XML.HtmlValueEncode(Expression.ToString(Item))));
                            }
                        }

                        Output.Append("</td>");
                    }

                    Output.Append("</tr>");
                }

                Output.Append("</tbody></table>");
            }
예제 #7
0
        /// <summary>
        /// Generates HTML for the markdown element.
        /// </summary>
        /// <param name="Output">HTML will be output here.</param>
        public override void GenerateHTML(StringBuilder Output)
        {
            object Result = this.EvaluateExpression();

            if (Result == null)
            {
                return;
            }

            SKImage Img;
            string  s;

            if (Result is Graph G)
            {
                using (SKImage Bmp = G.CreateBitmap(this.variables, out GraphSettings GraphSettings))
                {
                    SKData Data = Bmp.Encode(SKEncodedImageFormat.Png, 100);
                    byte[] Bin  = Data.ToArray();

                    s = System.Convert.ToBase64String(Bin, 0, Bin.Length);
                    s = "<img border=\"2\" width=\"" + GraphSettings.Width.ToString() + "\" height=\"" + GraphSettings.Height.ToString() +
                        "\" src=\"data:image/png;base64," + s + "\" />";

                    if (this.aloneInParagraph)
                    {
                        s = "<figure>" + s + "</figure>";
                    }

                    Data.Dispose();
                }
            }
            else if ((Img = Result as SKImage) != null)
            {
                using (SKData Data = Img.Encode(SKEncodedImageFormat.Png, 100))
                {
                    byte[] Bin = Data.ToArray();

                    s = System.Convert.ToBase64String(Bin, 0, Bin.Length);
                    s = "<img border=\"2\" width=\"" + Img.Width.ToString() + "\" height=\"" + Img.Height.ToString() +
                        "\" src=\"data:image/png;base64," + s + "\" />";

                    if (this.aloneInParagraph)
                    {
                        s = "<figure>" + s + "</figure>";
                    }
                }
            }
            else if (Result is Exception ex)
            {
                ex = Log.UnnestException(ex);

                if (ex is AggregateException ex2)
                {
                    StringBuilder sb = new StringBuilder();

                    foreach (Exception ex3 in ex2.InnerExceptions)
                    {
                        sb.Append("<p><font style=\"color:red\">");
                        sb.Append(XML.HtmlValueEncode(ex3.Message));
                        sb.AppendLine("</font></p>");
                    }

                    s = sb.ToString();
                }
                else
                {
                    s = "<font style=\"color:red\">" + XML.HtmlValueEncode(ex.Message) + "</font>";

                    if (this.aloneInParagraph)
                    {
                        s = "<p>" + s + "</p>";
                    }
                }
            }
            else
            {
                s = XML.HtmlValueEncode(Result.ToString());

                if (this.aloneInParagraph)
                {
                    s = "<p>" + s + "</p>";
                }
            }

            Output.Append(s);

            if (this.aloneInParagraph)
            {
                Output.AppendLine();
            }
        }
예제 #8
0
        /// <summary>
        /// Generates XAML for the markdown element.
        /// </summary>
        /// <param name="Output">XAML will be output here.</param>
        /// <param name="Settings">XAML settings.</param>
        /// <param name="TextAlignment">Alignment of text in element.</param>
        public override void GenerateXAML(XmlWriter Output, XamlSettings Settings, TextAlignment TextAlignment)
        {
            object Result = this.EvaluateExpression();

            if (Result == null)
            {
                return;
            }

            SKImage Img;
            string  s;

            if (Result is Graph G)
            {
                using (SKImage Bmp = G.CreateBitmap(this.variables))
                {
                    SKData Data = Bmp.Encode(SKEncodedImageFormat.Png, 100);
                    byte[] Bin  = Data.ToArray();

                    s = "data:image/png;base64," + System.Convert.ToBase64String(Bin, 0, Bin.Length);

                    // TODO: WPF does not support data URI scheme. Change to local temporary file.

                    Output.WriteStartElement("Image");
                    Output.WriteAttributeString("Source", s);
                    Output.WriteAttributeString("Width", Bmp.Width.ToString());
                    Output.WriteAttributeString("Height", Bmp.Height.ToString());
                    Output.WriteEndElement();

                    Data.Dispose();
                }
            }
            else if ((Img = Result as SKImage) != null)
            {
                using (SKData Data = Img.Encode(SKEncodedImageFormat.Png, 100))
                {
                    byte[] Bin = Data.ToArray();

                    s = "data:image/png;base64," + System.Convert.ToBase64String(Bin, 0, Bin.Length);

                    // TODO: WPF does not support data URI scheme. Change to local temporary file.

                    Output.WriteStartElement("Image");
                    Output.WriteAttributeString("Source", s);
                    Output.WriteAttributeString("Width", Img.Width.ToString());
                    Output.WriteAttributeString("Height", Img.Height.ToString());
                    Output.WriteEndElement();
                }
            }
            else if (Result is Exception ex)
            {
                ex = Log.UnnestException(ex);

                if (ex is AggregateException ex2)
                {
                    foreach (Exception ex3 in ex2.InnerExceptions)
                    {
                        Output.WriteStartElement("TextBlock");
                        Output.WriteAttributeString("TextWrapping", "Wrap");
                        Output.WriteAttributeString("Margin", Settings.ParagraphMargins);

                        if (TextAlignment != TextAlignment.Left)
                        {
                            Output.WriteAttributeString("TextAlignment", TextAlignment.ToString());
                        }

                        Output.WriteAttributeString("Foreground", "Red");
                        Output.WriteValue(ex.Message);
                        Output.WriteEndElement();
                    }
                }
                else
                {
                    if (this.aloneInParagraph)
                    {
                        Output.WriteStartElement("TextBlock");
                        Output.WriteAttributeString("TextWrapping", "Wrap");
                        Output.WriteAttributeString("Margin", Settings.ParagraphMargins);
                        if (TextAlignment != TextAlignment.Left)
                        {
                            Output.WriteAttributeString("TextAlignment", TextAlignment.ToString());
                        }
                    }
                    else
                    {
                        Output.WriteStartElement("Run");
                    }

                    Output.WriteAttributeString("Foreground", "Red");
                    Output.WriteValue(ex.Message);
                    Output.WriteEndElement();
                }
            }
            else
            {
                if (this.aloneInParagraph)
                {
                    Output.WriteStartElement("TextBlock");
                    Output.WriteAttributeString("TextWrapping", "Wrap");
                    Output.WriteAttributeString("Margin", Settings.ParagraphMargins);
                    if (TextAlignment != TextAlignment.Left)
                    {
                        Output.WriteAttributeString("TextAlignment", TextAlignment.ToString());
                    }
                }

                Output.WriteValue(Result.ToString());

                if (this.aloneInParagraph)
                {
                    Output.WriteEndElement();
                }
            }
        }
예제 #9
0
        static void HandleCaptcha(string Original, string Output, int Threshold, int MaxBlot)
        {
            // Loading image using path
            SKBitmap bitmap = SKBitmap.Decode(Original);

            // Converting to red/white format
            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    if (bitmap.GetPixel(i, j).Red > Threshold)
                    {
                        bitmap.SetPixel(i, j, SKColors.White);
                    }
                    else
                    {
                        bitmap.SetPixel(i, j, SKColors.Black);
                    }
                }
            }
            // Cleaning
            List <Point> points = new List <Point>(); // Current blot points
            List <Point> digits = new List <Point>(); // All digits points
            int          oldReviewPointsCount;

            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    if (bitmap.GetPixel(i, j) == SKColors.Black && !digits.Contains(new Point(i, j)))
                    {
                        points.Clear();
                        points.Add(new Point(i, j));
                        ExploreBlot();
                        if (points.Count <= MaxBlot)
                        {
                            ClearBlot();
                        }
                        else
                        {
                            digits.AddRange(points);
                        }
                    }
                }
            }
            // Saving
            SKImage    image  = SKImage.FromBitmap(bitmap);
            SKData     data   = image.Encode(SKEncodedImageFormat.Jpeg, 100);
            FileStream stream = new FileStream(Output, FileMode.Create, FileAccess.Write);

            data.SaveTo(stream);
            stream.Close();
            // Resources release
            stream.Dispose();
            data.Dispose();
            image.Dispose();
            bitmap.Dispose();
            // The end
            Console.Write("The work has been completed. Press any key to exit..");
            Console.ReadKey(true);

            void ExploreBlot()
            {
                oldReviewPointsCount = points.Count;
                List <Point> staticPoints = new List <Point>(points);

                for (int i = 0; i < staticPoints.Count; i++)
                {
                    Point a = new Point(staticPoints[i].X, staticPoints[i].Y);
                    if (bitmap.GetPixel(a.X - 1, a.Y - 1) == SKColors.Black && !points.Contains(new Point(a.X - 1, a.Y - 1)))
                    {
                        points.Add(new Point(a.X - 1, a.Y - 1));
                    }
                    if (bitmap.GetPixel(a.X, a.Y - 1) == SKColors.Black && !points.Contains(new Point(a.X, a.Y - 1)))
                    {
                        points.Add(new Point(a.X, a.Y - 1));
                    }
                    if (bitmap.GetPixel(a.X + 1, a.Y - 1) == SKColors.Black && !points.Contains(new Point(a.X + 1, a.Y - 1)))
                    {
                        points.Add(new Point(a.X + 1, a.Y - 1));
                    }
                    if (bitmap.GetPixel(a.X - 1, a.Y) == SKColors.Black && !points.Contains(new Point(a.X - 1, a.Y)))
                    {
                        points.Add(new Point(a.X - 1, a.Y));
                    }
                    if (bitmap.GetPixel(a.X + 1, a.Y) == SKColors.Black && !points.Contains(new Point(a.X + 1, a.Y)))
                    {
                        points.Add(new Point(a.X + 1, a.Y));
                    }
                    if (bitmap.GetPixel(a.X - 1, a.Y + 1) == SKColors.Black && !points.Contains(new Point(a.X - 1, a.Y + 1)))
                    {
                        points.Add(new Point(a.X - 1, a.Y + 1));
                    }
                    if (bitmap.GetPixel(a.X, a.Y + 1) == SKColors.Black && !points.Contains(new Point(a.X, a.Y + 1)))
                    {
                        points.Add(new Point(a.X, a.Y + 1));
                    }
                    if (bitmap.GetPixel(a.X + 1, a.Y + 1) == SKColors.Black && !points.Contains(new Point(a.X + 1, a.Y + 1)))
                    {
                        points.Add(new Point(a.X + 1, a.Y + 1));
                    }
                }
                if (points.Count != oldReviewPointsCount)
                {
                    ExploreBlot();
                }
            }

            void ClearBlot()
            {
                for (int i = 0; i < points.Count; i++)
                {
                    bitmap.SetPixel(points[i].X, points[i].Y, SKColors.White);
                }
            }
        }
예제 #10
0
        static void HandleImage(string Original, string Output)
        {
            SKBitmap Picture = SKBitmap.Decode(Original);

            SKBitmap[] Digits        = new SKBitmap[4];
            SKBitmap[] CroppedDigits = new SKBitmap[4];
            SKImage    Image         = SKImage.FromBitmap(Picture);

            for (int i = 0; i < 4; i++)
            {
                Digits[i]        = SKBitmap.FromImage(Image.Subset(new SKRectI(i * Picture.Width / 4, 0, (i + 1) * Picture.Width / 4, Picture.Height)));
                CroppedDigits[i] = Crop(Digits[i]);
                SKData     data   = SKImage.FromBitmap(CroppedDigits[i]).Encode(SKEncodedImageFormat.Jpeg, 100);
                FileStream stream = new FileStream(Output + i.ToString() + ".jpg", FileMode.Create, FileAccess.Write);
                data.SaveTo(stream);
                stream.Close();
                stream.Dispose();
                data.Dispose();
                Digits[i].Dispose();
                CroppedDigits[i].Dispose();
            }
            Image.Dispose();
            Picture.Dispose();
            Console.Write("The work has been completed. Press any key to exit..");
            Console.ReadKey(true);

            SKBitmap Crop(SKBitmap bitmap)
            {
                int width = bitmap.Width, height = bitmap.Height;
                int Left = 0, Top = 0, Right = 0, Bottom = 0;

                // Left
                for (int i = 0; i < width; i++)
                {
                    for (int j = 0; j < height; j++)
                    {
                        if (bitmap.GetPixel(i, j) == SKColors.Black)
                        {
                            Left = i;
                            i    = width;
                            break;
                        }
                    }
                }
                // Top
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        if (bitmap.GetPixel(j, i) == SKColors.Black)
                        {
                            Top = i;
                            i   = height;
                            break;
                        }
                    }
                }
                // Right
                for (int i = width; i > 0; i--)
                {
                    for (int j = 0; j < height; j++)
                    {
                        if (bitmap.GetPixel(i, j) == SKColors.Black)
                        {
                            Right = i;
                            i     = 0;
                            break;
                        }
                    }
                }
                // Bottom
                for (int i = height; i > 0; i--)
                {
                    for (int j = 0; j < width; j++)
                    {
                        if (bitmap.GetPixel(j, i) == SKColors.Black)
                        {
                            Bottom = i;
                            i      = 0;
                            break;
                        }
                    }
                }
                return(SKBitmap.FromImage(SKImage.FromBitmap(bitmap).Subset(new SKRectI(Left, Top, Right, Bottom))));
            }
        }
예제 #11
0
        /// <summary>
        /// Generates HTML for the markdown element.
        /// </summary>
        /// <param name="Output">HTML will be output here.</param>
        public override void GenerateHTML(StringBuilder Output)
        {
            object Result = this.EvaluateExpression();

            if (Result == null)
            {
                return;
            }

            string s;

            Graph   G = Result as Graph;
            SKImage Img;

            if (G != null)
            {
                GraphSettings GraphSettings = new GraphSettings();
                object        Obj;
                double        d;

                if (this.variables.TryGetVariable("GraphWidth", out Variable v) && (Obj = v.ValueObject) is double && (d = (double)Obj) >= 1)
                {
                    GraphSettings.Width       = (int)Math.Round(d);
                    GraphSettings.MarginLeft  = (int)Math.Round(15 * d / 640);
                    GraphSettings.MarginRight = GraphSettings.MarginLeft;
                }
                else if (!this.variables.ContainsVariable("GraphWidth"))
                {
                    this.variables["GraphWidth"] = (double)GraphSettings.Width;
                }


                if (this.variables.TryGetVariable("GraphHeight", out v) && (Obj = v.ValueObject) is double && (d = (double)Obj) >= 1)
                {
                    GraphSettings.Height        = (int)Math.Round(d);
                    GraphSettings.MarginTop     = (int)Math.Round(15 * d / 480);
                    GraphSettings.MarginBottom  = GraphSettings.MarginTop;
                    GraphSettings.LabelFontSize = 12 * d / 480;
                }
                else if (!this.variables.ContainsVariable("GraphHeight"))
                {
                    this.variables["GraphHeight"] = (double)GraphSettings.Height;
                }

                using (SKImage Bmp = G.CreateBitmap(GraphSettings))
                {
                    SKData Data = Bmp.Encode(SKEncodedImageFormat.Png, 100);
                    byte[] Bin  = Data.ToArray();

                    s = System.Convert.ToBase64String(Bin, 0, Bin.Length);
                    s = "<img border=\"2\" width=\"" + GraphSettings.Width.ToString() + "\" height=\"" + GraphSettings.Height.ToString() +
                        "\" src=\"data:image/png;base64," + s + "\" />";

                    if (this.aloneInParagraph)
                    {
                        s = "<figure>" + s + "</figure>";
                    }

                    Data.Dispose();
                }
            }
            else if ((Img = Result as SKImage) != null)
            {
                using (SKData Data = Img.Encode(SKEncodedImageFormat.Png, 100))
                {
                    byte[] Bin = Data.ToArray();

                    s = System.Convert.ToBase64String(Bin, 0, Bin.Length);
                    s = "<img border=\"2\" width=\"" + Img.Width.ToString() + "\" height=\"" + Img.Height.ToString() +
                        "\" src=\"data:image/png;base64," + s + "\" />";

                    if (this.aloneInParagraph)
                    {
                        s = "<figure>" + s + "</figure>";
                    }
                }
            }
            else if (Result is Exception ex)
            {
                ex = Log.UnnestException(ex);

                if (ex is AggregateException ex2)
                {
                    StringBuilder sb = new StringBuilder();

                    foreach (Exception ex3 in ex2.InnerExceptions)
                    {
                        sb.Append("<p><font style=\"color:red\">");
                        sb.Append(XML.HtmlValueEncode(ex3.Message));
                        sb.AppendLine("</font></p>");
                    }

                    s = sb.ToString();
                }
                else
                {
                    s = "<font style=\"color:red\">" + XML.HtmlValueEncode(ex.Message) + "</font>";

                    if (this.aloneInParagraph)
                    {
                        s = "<p>" + s + "</p>";
                    }
                }
            }
            else
            {
                s = XML.HtmlValueEncode(Result.ToString());

                if (this.aloneInParagraph)
                {
                    s = "<p>" + s + "</p>";
                }
            }

            Output.Append(s);

            if (this.aloneInParagraph)
            {
                Output.AppendLine();
            }
        }
예제 #12
0
        /// <summary>
        /// Generates XAML for the markdown element.
        /// </summary>
        /// <param name="Output">XAML will be output here.</param>
        /// <param name="Settings">XAML settings.</param>
        /// <param name="TextAlignment">Alignment of text in element.</param>
        public override void GenerateXAML(XmlWriter Output, XamlSettings Settings, TextAlignment TextAlignment)
        {
            object Result = this.EvaluateExpression();

            if (Result == null)
            {
                return;
            }

            Graph   G = Result as Graph;
            SKImage Img;
            string  s;

            if (G != null)
            {
                GraphSettings GraphSettings = new GraphSettings();
                object        Obj;
                double        d;

                if (this.variables.TryGetVariable("GraphWidth", out Variable v) && (Obj = v.ValueObject) is double && (d = (double)Obj) >= 1)
                {
                    GraphSettings.Width       = (int)Math.Round(d);
                    GraphSettings.MarginLeft  = (int)Math.Round(15 * d / 640);
                    GraphSettings.MarginRight = GraphSettings.MarginLeft;
                }
                else if (!this.variables.ContainsVariable("GraphWidth"))
                {
                    this.variables["GraphWidth"] = (double)Settings.DefaultGraphWidth;
                    GraphSettings.MarginLeft     = (int)Math.Round(15.0 * Settings.DefaultGraphWidth / 640);
                    GraphSettings.MarginRight    = GraphSettings.MarginLeft;
                }

                if (this.variables.TryGetVariable("GraphHeight", out v) && (Obj = v.ValueObject) is double && (d = (double)Obj) >= 1)
                {
                    GraphSettings.Height        = (int)Math.Round(d);
                    GraphSettings.MarginTop     = (int)Math.Round(15 * d / 480);
                    GraphSettings.MarginBottom  = GraphSettings.MarginTop;
                    GraphSettings.LabelFontSize = 12 * d / 480;
                }
                else if (!this.variables.ContainsVariable("GraphHeight"))
                {
                    this.variables["GraphHeight"] = (double)Settings.DefaultGraphHeight;
                    GraphSettings.MarginTop       = (int)Math.Round(15.0 * Settings.DefaultGraphHeight / 480);
                    GraphSettings.MarginBottom    = GraphSettings.MarginTop;
                }

                using (SKImage Bmp = G.CreateBitmap(GraphSettings))
                {
                    SKData Data = Bmp.Encode(SKEncodedImageFormat.Png, 100);
                    byte[] Bin  = Data.ToArray();

                    s = "data:image/png;base64," + System.Convert.ToBase64String(Bin, 0, Bin.Length);

                    // TODO: WPF does not support data URI scheme. Change to local temporary file.

                    Output.WriteStartElement("Image");
                    Output.WriteAttributeString("Source", s);
                    Output.WriteAttributeString("Width", Bmp.Width.ToString());
                    Output.WriteAttributeString("Height", Bmp.Height.ToString());
                    Output.WriteEndElement();

                    Data.Dispose();
                }
            }
            else if ((Img = Result as SKImage) != null)
            {
                using (SKData Data = Img.Encode(SKEncodedImageFormat.Png, 100))
                {
                    byte[] Bin = Data.ToArray();

                    s = "data:image/png;base64," + System.Convert.ToBase64String(Bin, 0, Bin.Length);

                    // TODO: WPF does not support data URI scheme. Change to local temporary file.

                    Output.WriteStartElement("Image");
                    Output.WriteAttributeString("Source", s);
                    Output.WriteAttributeString("Width", Img.Width.ToString());
                    Output.WriteAttributeString("Height", Img.Height.ToString());
                    Output.WriteEndElement();
                }
            }
            else if (Result is Exception ex)
            {
                ex = Log.UnnestException(ex);

                if (ex is AggregateException ex2)
                {
                    foreach (Exception ex3 in ex2.InnerExceptions)
                    {
                        Output.WriteStartElement("TextBlock");
                        Output.WriteAttributeString("TextWrapping", "Wrap");
                        Output.WriteAttributeString("Margin", Settings.ParagraphMargins);

                        if (TextAlignment != TextAlignment.Left)
                        {
                            Output.WriteAttributeString("TextAlignment", TextAlignment.ToString());
                        }

                        Output.WriteAttributeString("Foreground", "Red");
                        Output.WriteValue(ex.Message);
                        Output.WriteEndElement();
                    }
                }
                else
                {
                    if (this.aloneInParagraph)
                    {
                        Output.WriteStartElement("TextBlock");
                        Output.WriteAttributeString("TextWrapping", "Wrap");
                        Output.WriteAttributeString("Margin", Settings.ParagraphMargins);
                        if (TextAlignment != TextAlignment.Left)
                        {
                            Output.WriteAttributeString("TextAlignment", TextAlignment.ToString());
                        }
                    }
                    else
                    {
                        Output.WriteStartElement("Run");
                    }

                    Output.WriteAttributeString("Foreground", "Red");
                    Output.WriteValue(ex.Message);
                    Output.WriteEndElement();
                }
            }
            else
            {
                if (this.aloneInParagraph)
                {
                    Output.WriteStartElement("TextBlock");
                    Output.WriteAttributeString("TextWrapping", "Wrap");
                    Output.WriteAttributeString("Margin", Settings.ParagraphMargins);
                    if (TextAlignment != TextAlignment.Left)
                    {
                        Output.WriteAttributeString("TextAlignment", TextAlignment.ToString());
                    }
                }

                Output.WriteValue(Result.ToString());

                if (this.aloneInParagraph)
                {
                    Output.WriteEndElement();
                }
            }
        }