DPI for Generated Image seems lower than I'd like, and particularly fuzzy scaled down
Closed this issue · 2 comments
Hi, so I'm a Discord bot developer and I'm attempting to write a LaTeX compilation command for my bot that will produce a PNG. I came across this library and it seemed to do everything I needed. So, I am attempting to use it. I ended up producing a command that looks like so
[Command("tex")]
[Description("Produce a PNG of the LaTeX code in the code block. Note that the LaTeX must be in a code block like so\n\\`\\`\\`\nLaTeX here\n\\`\\`\\`")]
[BotCategory(BotCategory.Evaluation)]
public async Task EvaluateTex(CommandContext context, [RemainingText][Description("The LaTeX code in a code block to render as an image")] string latex)
{
// generate LaTeX string and determine whether this is supposed to be in light mode
TextPainter painter = new TextPainter {
LaTeX = latex[upperBound..lowerBound],
FontSize = 20,
TextColor = lightMode ? SKColors.Black : SKColors.White,
};
RectangleF bounds = painter.Measure(400);
using SKSurface surface = SKSurface.Create(new SKImageInfo((int)Math.Ceiling(bounds.Width)+50, (int)Math.Ceiling(bounds.Height)+50));
using SKCanvas canvas = surface.Canvas;
canvas.Clear(lightMode ? SKColors.White : SKColors.Black);
painter.Draw(canvas, new PointF(25, 25), 400);
SKData snapshot = surface.Snapshot().Encode(SKEncodedImageFormat.Png, 100);
using Stream png = snapshot.AsStream();
DiscordMessageBuilder builder = new DiscordMessageBuilder().WithFile("latex.png", png);
await context.RespondAsync(builder);
}
When I give the input
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur iaculis viverra est id sagittis. Morbi quis turpis ac justo accumsan elementum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. In sed varius arcu. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer a metus et tellus condimentum dignissim at sed purus. Cras non congue mauris, non pulvinar ante. Praesent dictum consequat nunc vel posuere. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent est lacus, dictum non malesuada vitae, mattis sit amet libero. Mauris pellentesque semper dui a rutrum. Mauris sit amet aliquet erat. Nulla nibh sapien, aliquet sed pharetra vitae, sagittis a odio. Cras ac viverra arcu. Morbi tortor quam, gravida nec placerat in, sagittis vel tellus.
I get the output
which isn't terrible and is still legible, but is still more difficult to read than I'd like, particularly when it gets scaled down by Discord. If anyone has a solution that would be great.
I needed the image produced to be on a plain background of either white or black so I used an SKSurface. However, I can't figure out how to increase the DPI, or just produce a better image. I might be posting in the wrong place as this may just be me misusing SkiaSharp but figured I might as well ask here. Let me know if I am doing something completely wrong. Thanks for any help that y'all can give me.
You can
- Disable antialiasing but will produce rough edges in each pixel if you zoom in
- Draw a larger image but will use more memory and storage space.
Thanks for the response. As long as it's not over 8 mb I can upload it and I don't actually store it so that might be the best solution I can get.