Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use wpf-math to generate an exact size bitmap. #118

Closed
bradphelan opened this issue Mar 12, 2018 · 8 comments
Closed

How to use wpf-math to generate an exact size bitmap. #118

bradphelan opened this issue Mar 12, 2018 · 8 comments
Assignees

Comments

@bradphelan
Copy link

See question at Stack Overflow

https://stackoverflow.com/questions/9264398/how-to-calculate-wpf-textblock-width-for-its-known-font-size-and-characters

@ForNeVeR
Copy link
Owner

Please clarify. Do you want to generate an image of M x N pixels or do you want to know the size of a formula image?

@bradphelan
Copy link
Author

I want to know the size of the formula image. I intend to capture the image and send it as part of a web service. My current work around is to render the formula into an oversize image and the auto crop the whitespace. I'm not sure if it is possible to do better.

@bradphelan
Copy link
Author

@ForNeVeR
Copy link
Owner

Alright, now I understand the question. Will try to answer ASAP. Not sure if we have all the necessary parts to get the image size.

Note: honestly, I don't think that it's the best idea to use WPF in a web service. Although I don't know of any other options if you want to render the LaTeX formula in your service. Hopefully, we'll be able to back you up with WPFless cross-platform solution some day, please stay tuned!

@bradphelan
Copy link
Author

Thanks for the feedback. The tool is just a toy at the moment. I was looking to see if it is possible to render math into slack channels and that was a good excuse to write a slack bot. And then I needed a tek renderer and I fell across your project. Maybe I might change to something like

http://quicklatex.com/

@ForNeVeR
Copy link
Owner

ForNeVeR commented Mar 25, 2018

Hello! I'm sorry for the long pause: it's just too much going on now, so I couldn't follow the events quickly enough. I always remember my promises though, and trying to get back to the WPF-Math discussions right now.

To get a size of the image (and also to turn it into a PNG in a bit more elegant way), consider using a following C# snippet:

using System;
using System.IO;
using System.Windows.Media.Imaging;
using WpfMath;

namespace ConsoleApplication2
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            const string latex = @"\frac{2+2}{2}";
            const string fileName = @"T:\Temp\formula.png";
            
            var parser = new TexFormulaParser();
            var formula = parser.Parse(latex);
            var renderer = formula.GetRenderer(TexStyle.Display, 20.0, "Arial");
            var bitmapSource = renderer.RenderToBitmap(0, 0);
            Console.WriteLine($"Image width: {bitmapSource.Width}");
            Console.WriteLine($"Image height: {bitmapSource.Height}");
            
            var encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
            using (var target = new FileStream(fileName, FileMode.Create))
            {
                encoder.Save(target);
                Console.WriteLine($"File saved to {fileName}");
            }
        }
    }
}

Also there's an extension method formula.RenderToPng that essentially does the same but returns a byte array: https://github.com/ForNeVeR/wpf-math/blob/c0a7595a061a02db9a1c31724ae264688f72f61c/src/WpfMath/Extensions.cs#L8-L26

Also you might consider using an geometry renderer and turn the image into SVG. To do that, use renderer.RenderToGeometry(…) and then see the example: https://github.com/ForNeVeR/wpf-math/blob/c0a7595a061a02db9a1c31724ae264688f72f61c/src/WpfMath.Example/MainWindow.xaml.cs#L58-L63

@ForNeVeR
Copy link
Owner

I consider the task solved and will close the issue for now. Although feel free to ask any additional questions, I'll do my best to help you!

@bradphelan
Copy link
Author

bradphelan commented Mar 26, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants