如何使用 System.Drawing 将旋转的字符串绘制为图像?
我正在将字符串绘制到图像上。图像的大小是动态的,或者换句话说,图像的大小是显示字符串所需的大小。为了实现这一点,我在使用 Graphics.DrawString() 渲染文本之前使用 Graphics.MeasureString() 测量大小。
在轮换发挥作用之前一切都很好。到目前为止,我将字符串绘制到位图并旋转得到的整个位图。
问题是我的调色板非常有限并且没有混合颜色。因此,我必须避免任何类型的抗锯齿,这只能通过使用 InterpolationMode.NearestNeighbor 通过旋转文本位图来实现。虽然这确保了不会渲染出不需要的颜色,但结果确实非常难看(从用户的角度来看)。
我的想法:应该可以通过使用 Graphics.RotateTransform() 旋转文本来将文本绘制到位图并避免剪切,不是吗?
因为我必须首先定义要绘制的图像的大小,并且因为该大小通过旋转而增加,所以我不知道如何完成此操作。
非常感谢任何帮助!
I am drawing strings to images. The size of the images is dynamic, or in other words, the images are as large as necessary to display the strings. To achieve that I am measuring the size with Graphics.MeasureString() before rendering the text with Graphics.DrawString().
That's all fine till the point where rotation comes into play. Until now I am drawing the string to the bitmap and rotate the whole bitmap I get as an result.
The problem is that I have a very limited color palette and no mixed colors. So I have to avoid any kind of antialiasing, which is only possible by using InterpolationMode.NearestNeighbor by rotating the text bitmap. While that makes sure no unwanted color is rendered the result is indeed very ugly (from the users point of view).
My thought: it should be possible to draw the text to the bitmap by rotating it with Graphics.RotateTransform() and avoiding clipping, isn't it?
Because I have to define the size of the image to draw on first, and because this size increases by rotating, I have no clue how to get this done.
Any help is very appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的解决方案是一个可执行的 MVC 操作:
My solution as an executable MVC action:
这段代码会给你一个想法:
这是一种使用 GraphicsPath 测量旋转文本边界的方法。逻辑很简单,GraphicsPath 将文本转换为点列表,然后计算边界矩形。
测试代码:
This code will give you an idea:
Here is a method to measure rotated text bounds by using GraphicsPath. Logic is simple, GraphicsPath converts text to point list and then it calculates bounds rectangle.
Test code: