位图文本背景和水平对齐
我将文本叠加到位图上,如下所示,我需要能够在文本后面设置背景颜色,并设置水平对齐(即左/右/中心),任何人都可以告诉我如何做到这一点。另请注意,文本大小可能会有所不同。
谢谢。
Bitmap frameBitmap = new Bitmap(streamFrameWidth, streamFrameHeight,
streamFrameWidth * 3,
System.Drawing.Imaging.PixelFormat.Format24bppRgb, pFrame);
using (Graphics g = Graphics.FromImage(frameBitmap))
{
// Create font and brush.
Font drawFont = new Font("Arial", 12, FontStyle.Bold);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Create point for upper-left corner of drawing.
PointF drawPoint = new PointF(10.0F, 40.0F);
//HorizontalAlignment.
// draw the text
g.DrawString(overlayText, drawFont, drawBrush, drawPoint);
}
I am overlaying text to a bitmap as shown below, I need to be able to set a background colour behind the text and also set the HorizontalAlignment (i.e left/right/centre), can anyone advise me how this can be done. Also note the text size can vary.
Thanks.
Bitmap frameBitmap = new Bitmap(streamFrameWidth, streamFrameHeight,
streamFrameWidth * 3,
System.Drawing.Imaging.PixelFormat.Format24bppRgb, pFrame);
using (Graphics g = Graphics.FromImage(frameBitmap))
{
// Create font and brush.
Font drawFont = new Font("Arial", 12, FontStyle.Bold);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Create point for upper-left corner of drawing.
PointF drawPoint = new PointF(10.0F, 40.0F);
//HorizontalAlignment.
// draw the text
g.DrawString(overlayText, drawFont, drawBrush, drawPoint);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 DrawString 方法的 StringFormat 参数来控制绘制文本的对齐方式。
You can control the alignment of the drawn text by using the StringFormat parameter of the DrawString method.
您可能需要
TextRenderer.MeasureText
。它返回要显示的文本的大小。将文本的大小与位图的大小相结合,您可以根据所需的 HorizontalAlignment 计算出文本的适当位置。
一旦知道文本的边界(大小和位置),您可以简单地在这些边界上绘制颜色以实现背景颜色,然后再在顶部绘制文本。
You probably need
TextRenderer.MeasureText
.It returns the size of the text to be displayed. Combining the size of the text with the size of the Bitmap, you can work out the appropriate location of the text based upon the required HorizontalAlignment.
Once you know the bounds (size and location) of the text, you can simply paint a color to those bounds to implement a background colour before drawing the text on top.