private void Parse_MouseDown(object sender, MouseButtonEventArgs e) { canvasString = InputTextBox.Text; canvasString = canvasString.Replace(" ", ""); if (canvasString == "") { imageCanvas.Source = null; return; } if (ParseFormula()) { imageCanvas.Source = null; return; } TexFormula formula = Parse_Formula(canvasString); if (formula == null) { return; } TexRenderer renderer = formula.GetRenderer(TexStyle.Display, 20.0, "Arial"); savePng = renderer.RenderToBitmap(0.0, 0.0); imageCanvas.Source = savePng; }
/// <summary> /// Renders a formula to a bitmap. /// </summary> /// <param name="formulaRenderer">The formula renderer.</param> /// <param name="x">The x offset of the formula.</param> /// <param name="y">The y offset. /// If y is negative, the distance of top to text does not change, and the distance from bottom to text changes by Ceiling(-y), because Ceiling(-y) pixel lines are added to the image. /// That means, that if looked from the bottom of the image, the formula shifts upwardes, but in integer pixel steps. /// If y is positive, the distance of top of image to text changes by y. Because Ceiling(y) pixel lines are added to the image, /// the distance from bottom of the image to text changes by [Ceiling(y)-y], i.e. the shift is 0 for y==0, 1 for y==(0+epsilon), and 0 again for y==1. /// That means, if measured from the bottom of the image, the formula shifts upwards, but maximal by 1 pixel. /// </param> /// <param name="dpiResolution">The resolution of the image in dpi. If not sure, use 96 dpi.</param> /// <returns>The bitmap souce that represents the formula.</returns> public static (BitmapSource bitmapSource, int width96thInch, int heigth96thInch) RenderToBitmap(TexRenderer formulaRenderer, double x, double y, double dpiResolution) { var visual = new DrawingVisual(); using (var drawingContext = visual.RenderOpen()) { // Note that argument y in Render measures from the top. Thus is y is positive, this shifts the text down. formulaRenderer.Render(drawingContext, x, Math.Max(0, y)); // if y negative, then we don't change y, because we measure relative to the upper edge of the image. Only if y is positive, we translate the formula downwards. } var width = (int)Math.Ceiling(formulaRenderer.RenderSize.Width); var height = (int)Math.Ceiling(formulaRenderer.RenderSize.Height); height += (int)Math.Ceiling(Math.Abs(y)); var relativeResolution = dpiResolution / 96.0; var bitmap = new RenderTargetBitmap((int)(relativeResolution * width), (int)(relativeResolution * height), (int)(relativeResolution * 96), (int)(relativeResolution * 96), PixelFormats.Default); bitmap.Render(visual); return(bitmap, width, height); }
private Rect ExtractAreaOfLine(float scale, TexRenderer f) { return(new Rect(f.X * scale, (f.Y * scale - lineDepth), f.Width * scale, lineHeight)); }