Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

How to access or generate TextStyle preview programmatically?

varshaauti27
Advocate

How to access or generate TextStyle preview programmatically?

varshaauti27
Advocate
Advocate

Is there a way to preview a TextStyle in Civil 3D using C#.net?

varshaauti27_0-1734072574953.png

Any insights or suggestions would be greatly appreciated. Thank you!

0 Likes
Reply
167 Views
4 Replies
Replies (4)

hippe013
Advisor
Advisor

You can use the DrawString method of the System.Drawing.Graphics to draw onto a bitmap image from a given string, font, brush, and point. 

 

Here is a snippet of code from my repos with some unnecessary lines removed. 

 

Using bmp As New Bitmap(200, 40)
   Using g As Drawing.Graphics = System.Drawing.Graphics.FromImage(bmp)
      Dim size As SizeF = g.MeasureString(msg, font)
      g.Clear(Color.Magenta) 'Used to Create Transparency
      g.FillRectangle(brushWhite, 0, 0, size.Width + 6, size.Height + 4)
      g.DrawRectangle(blackPen, 1, 1, size.Width + 2, size.Height + 2)
      g.DrawString(msg, font, brushBlack, New PointF(2, 2))
   End Using
End Using

 

 

 

 

0 Likes

varshaauti27
Advocate
Advocate

I'm looking for a method to preview different text styles. Instead of generating an image with specific text content, I want to explore and visualize various text styles.

 

varshaauti27_0-1734108070675.png

 

0 Likes

hippe013
Advisor
Advisor

I am confused. What you have circled is an image displayed in a dialog box of the selected text style. The code that I posted could generate that image. So, with that said, I guess I am not sure what you mean when you say you want to explore and Visualize various text styles. So, what do you mean when you want to preview the text style? 

0 Likes

varshaauti27
Advocate
Advocate

The sample code does not generate the same preview image as displayed in the TextStyle window.

Refer attached image for reference.

varshaauti27_0-1734126670583.png

 

However, I was able to replicate the correct preview image by following these steps:

  1. Opening a new drawing.
  2. Adding the required text.
  3. Assigning the desired text style.
  4. Using the CapturePreviewImage method of the Document class to capture the image.

While this approach works, I am seeking an alternative method that:

  • Does not require creating a new drawing.
  • Generates the exact preview image as shown in the TextStyle window.

Could you suggest any such alternatives or optimizations?

0 Likes