C++ 在GDI+中在画布上绘制图像时,如何获取单词长度和高度;

C++ 在GDI+中在画布上绘制图像时,如何获取单词长度和高度;,c++,gdi+,drawstring,outer-join,boundary,C++,Gdi+,Drawstring,Outer Join,Boundary,我在画布上画一个字符串,使用C++中的GDI+。 是否有API可以获取特定字体中字符串的外层(宽度、高度)? 非常感谢! 非常感谢Windows程序员的解决方案。 我编写了以下代码 Bitmap bitmap(1000,1000); Graphics graphics(&bitmap); RectF rec; RectF useless; graphics.MeasureString(m_sWords, -1, m_pFont.get(), use

我在画布上画一个字符串,使用C++中的GDI+。 是否有API可以获取特定字体中字符串的外层(宽度、高度)? 非常感谢! 非常感谢Windows程序员的解决方案。 我编写了以下代码

    Bitmap bitmap(1000,1000);
    Graphics graphics(&bitmap);
    RectF rec;
    RectF useless;
    graphics.MeasureString(m_sWords, -1, m_pFont.get(), useless, &rec);
    int WordWidth = rec.Width + 1;
    int WordHeight Height = rec.Height + 1;

我需要使用真实的图形来调用MeasureString吗?有没有办法在不创建大型图形实例的情况下获得wordwidth和wordheight?我发现这是资源消耗。

Graphics::MeasureString计算近似值。

Graphics::MeasureString计算近似值。

不幸的是,您确实需要使用
Graphics
对象来执行此操作

我使用的C代码(返回一个
矩形f
,因为我想知道宽度和高度)如下所示:

/// <summary> The text bounding box. </summary>
private static readonly RectangleF __boundingBox = new RectangleF(29, 25, 90, 40);

/// <summary>
///    Gets the width of a given string, in the given font, with the given
///    <see cref="StringFormat"/> options.
/// </summary>
/// <param name="text">The string to measure.</param>
/// <param name="font">The <see cref="Font"/> to use.</param>
/// <param name="fmt">The <see cref="StringFormat"/> to use.</param>
/// <returns> The floating-point width, in pixels. </returns>
private static RectangleF GetStringBounds(string text, Font font,
   StringFormat fmt)
{
   CharacterRange[] range = { new CharacterRange(0, text.Length) };
   StringFormat myFormat = fmt.Clone() as StringFormat;
   myFormat.SetMeasurableCharacterRanges(range);

   using (Graphics g = Graphics.FromImage(
       new Bitmap((int) __boundingBox.Width, (int) __boundingBox.Height)))
   {
      Region[] regions = g.MeasureCharacterRanges(text, font,
         __boundingBox, myFormat);
      return regions[0].GetBounds(g);
   }
}
///文本边界框。
私有静态只读矩形F__边界框=新矩形F(29,25,90,40);
/// 
///获取给定字符串的宽度,该字符串使用给定的字体,并具有给定的
///选项。
/// 
///要测量的字符串。
///要使用的。
///要使用的。
///浮点宽度,以像素为单位。
私有静态矩形GetStringBounds(字符串文本、字体、,
字符串格式(fmt)
{
CharacterRange[]range={newcharacterRange(0,text.Length)};
StringFormat myFormat=fmt.Clone()作为StringFormat;
myFormat.SetMeasureCharacterRanges(范围);
使用(Graphics g=Graphics.FromImage(
新位图((int)\uuuBoundingBox.Width,(int)\uuBoundingBox.Height)))
{
Region[]regions=g.MeasureCharacterRanges(文本、字体、,
__边框,myFormat);
返回区域[0].GetBounds(g);
}
}
这将返回整个文本字符串大小的
矩形f
,根据指定为
\u boundingBox
的边界框,根据需要对单词进行包装。另一方面,
Graphics
对象将在
using
语句完成后立即销毁


另一方面,GDI+在这方面似乎相当不可靠;我发现它相当有缺陷(例如,参见)。如果您可以从
System.Windows.Forms
使用,那么请使用。

不幸的是,您确实需要使用
图形
对象来执行此操作

我使用的C代码(返回一个
矩形f
,因为我想知道宽度和高度)如下所示:

/// <summary> The text bounding box. </summary>
private static readonly RectangleF __boundingBox = new RectangleF(29, 25, 90, 40);

/// <summary>
///    Gets the width of a given string, in the given font, with the given
///    <see cref="StringFormat"/> options.
/// </summary>
/// <param name="text">The string to measure.</param>
/// <param name="font">The <see cref="Font"/> to use.</param>
/// <param name="fmt">The <see cref="StringFormat"/> to use.</param>
/// <returns> The floating-point width, in pixels. </returns>
private static RectangleF GetStringBounds(string text, Font font,
   StringFormat fmt)
{
   CharacterRange[] range = { new CharacterRange(0, text.Length) };
   StringFormat myFormat = fmt.Clone() as StringFormat;
   myFormat.SetMeasurableCharacterRanges(range);

   using (Graphics g = Graphics.FromImage(
       new Bitmap((int) __boundingBox.Width, (int) __boundingBox.Height)))
   {
      Region[] regions = g.MeasureCharacterRanges(text, font,
         __boundingBox, myFormat);
      return regions[0].GetBounds(g);
   }
}
///文本边界框。
私有静态只读矩形F__边界框=新矩形F(29,25,90,40);
/// 
///获取给定字符串的宽度,该字符串使用给定的字体,并具有给定的
///选项。
/// 
///要测量的字符串。
///要使用的。
///要使用的。
///浮点宽度,以像素为单位。
私有静态矩形GetStringBounds(字符串文本、字体、,
字符串格式(fmt)
{
CharacterRange[]range={newcharacterRange(0,text.Length)};
StringFormat myFormat=fmt.Clone()作为StringFormat;
myFormat.SetMeasureCharacterRanges(范围);
使用(Graphics g=Graphics.FromImage(
新位图((int)\uuuBoundingBox.Width,(int)\uuBoundingBox.Height)))
{
Region[]regions=g.MeasureCharacterRanges(文本、字体、,
__边框,myFormat);
返回区域[0].GetBounds(g);
}
}
这将返回整个文本字符串大小的
矩形f
,根据指定为
\u boundingBox
的边界框,根据需要对单词进行包装。另一方面,
Graphics
对象将在
using
语句完成后立即销毁


另一方面,GDI+在这方面似乎相当不可靠;我发现它相当有缺陷(例如,参见)。如果您可以使用from
System.Windows.Forms
,则执行。

以使图片完整:只需使用GraphicsPath即可完成,因为它不需要DeviceContext:

Dim p As New GraphicsPath

Using stringFormat As New StringFormat()
    stringFormat.Trimming = StringTrimming.EllipsisCharacter
    stringFormat.LineAlignment = StringAlignment.Center
    stringFormat.Alignment = StringAlignment.Near

    p.AddString(text, font.FontFamily, font.Style, font.SizeInPoints, Point.Empty, stringFormat)
End Using

Return p.GetBounds.Size

其中,文本是给定的字符串,字体是给定的字体。返回SizeF结构。我发现结果比图形精确得多。MeasureString又名GdipMeasureString API。

要使图片完整,只需使用GraphicsPath即可,这样做更好,因为它不需要设备上下文:

Dim p As New GraphicsPath

Using stringFormat As New StringFormat()
    stringFormat.Trimming = StringTrimming.EllipsisCharacter
    stringFormat.LineAlignment = StringAlignment.Center
    stringFormat.Alignment = StringAlignment.Near

    p.AddString(text, font.FontFamily, font.Style, font.SizeInPoints, Point.Empty, stringFormat)
End Using

Return p.GetBounds.Size
其中,文本是给定的字符串,字体是给定的字体。返回SizeF结构。我发现结果比Graphics.MeasureString aka GdipMeasureString API精确得多