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
Comments
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? |
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. |
You can find the current code at https://github.com/bradphelan/slackjax/blob/master/Renderer/Renderer.cs#L37 |
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! |
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 |
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 Also you might consider using an geometry renderer and turn the image into SVG. To do that, use |
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! |
Thankyou :)
…On Sun, 25 Mar 2018, 1:36 PM Friedrich von Never ***@***.***> wrote:
Closed #118 <#118>.
—
You are receiving this because you were assigned.
Reply to this email directly, view it on GitHub
<#118 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AABE8hfkr68qxZzFs9viJX4oBuMcvi4Fks5th4FMgaJpZM4SmLUa>
.
|
See question at Stack Overflow
https://stackoverflow.com/questions/9264398/how-to-calculate-wpf-textblock-width-for-its-known-font-size-and-characters
The text was updated successfully, but these errors were encountered: