如何将弯曲文本渲染为位图?
我当前正在动态创建位图,并使用位图中的图形对象在其上绘制字符串,如下所示:
System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
graph.DrawString(text, font, brush, new System.Drawing.Point(0, 0));
这将返回一个矩形位图,其中字符串从左到右直接书写。 我也希望能够把绳子画成彩虹的形状。 我该怎么做?
I am currently dynamically creating a bitmap and using the graphics object from the bitmap to draw a string on it like so:
System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
graph.DrawString(text, font, brush, new System.Drawing.Point(0, 0));
This returns a rectangular bitmap with the string written straight across from left to right.
I would like to also be able to draw the string in the shape of a rainbow.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我最近遇到了这个问题(我正在渲染文本以打印到 CD 上),所以这是我的解决方案:
I recently had this problem (I was rendering text for printing onto CDs), so here's my solution:
我需要用原始 C# 来回答这个问题,但找不到任何可以这样做的示例:
这个解决方案需要大量的数学知识来解决。总之,它需要一组点(2D 向量),将它们与基线对齐,然后围绕样条曲线弯曲这些点。该代码足够快,可以实时更新并处理循环等。
为了方便起见,该解决方案使用 GraphicsPath.AddString 将文本转换为矢量,并使用 GraphicsPath.PathPoints/PathTypes 作为点,但是您可以使用相同的功能。 (不过我不建议实时制作 4k 位图)。
该代码有一个简单的 Paint 函数,后面是 Spline 类。 GraphicsPath 在 Paint 方法中使用是为了使代码更容易理解。 ClickedPoints 是您希望文本弯曲的点的数组。我使用了一个列表,因为它被添加到鼠标事件中,如果您事先知道这些点,请使用数组。
现在是样条线类:
I needed to answer this question in raw C# and could not find any samples that do it so:
This solution requires a lot of Maths to solve. In summary it takes a set of points (2D vectors), aligns them to a baseline and then bends the points around a Spline. The code is fast enough to update in real time and handles loops, etc.
For ease the solution turns text into vectors using GraphicsPath.AddString and uses GraphicsPath.PathPoints/PathTypes for the points, however you can bend any shape or even bitmaps using the same functions. (I would not recommend doing 4k bitmaps in real time though).
The code has a simple Paint function followed by the Spline class. GraphicsPath is used in the Paint method to make the code hopefully easier to understand. ClickedPoints are the array of points you want the text bent around. I used a List as it was added to in Mouse events, use an array if you know the points beforehand.
And now for the spline class:
我认为唯一的方法是单独渲染每个字符并使用 来
旋转文本。您需要自己计算出旋转角度和渲染偏移。您可以使用
来获取每个字符的大小。
I think the only way is to render each character individually and use the
to rotate the text. You'll need to work out the rotation angle and rendering offset yourself. You can use the
to get the size of each character.
不幸的是,在 GDI+ 中,无法将字符串附加到路径(这正是您所需要的)。
因此,做到这一点的唯一方法是“手工”完成。这意味着将字符串拆分为字符,并根据您自己的路径计算来放置它们。
除非您想为此投入大量工作,否则您应该尝试找到一个库(可能完全替代 GDI+)来完成此操作,否则就放弃您的彩虹。
使用 WPF,您可以在路径上呈现文本(查看操作方法链接)
Unfortunatelly in GDI+ there is no way to attach Strings to a path (this is what you would be looking for).
So the only way to do this is doing it "by hand". That means splitting up the string into characters and placing them based on your own path calculations.
Unless you want to put a lot of work into this you should try to find a library (potentially complete GDI+ replacement) to do this or give up on your rainbow.
With WPF you can render text on a path (see link for a howto)