5281

VB.NET: Finding the size of text to be drawn, before it is drawn to a control

Question:

I'm creating a user control and adding a text size property to it. I need to know how big the text size is going to be before drawing it on the control so that I can center it on the control in relation to its height and width.

I'm under the assumption that this is a windows API command. However I have not been able to locate any information on it.

Public Sub DrawImage(ByVal gr As Graphics) If Me.Image Is Nothing Then Return Dim r As Rectangle = New Rectangle(8, 8, Me.ImageSize.Width, Me.ImageSize.Height) Select Case Me.ImageAlign Case ContentAlignment.TopLeft r = New Rectangle(4, 4, Me.ImageSize.Width, Me.ImageSize.Height) Case ContentAlignment.TopCenter r = New Rectangle((Me.Width / 2 - Me.ImageSize.Width / 2) / 2, 4, Me.ImageSize.Width, Me.ImageSize.Height) Case ContentAlignment.TopRight r = New Rectangle(Me.Width - 4 - Me.ImageSize.Width, 4, Me.ImageSize.Width, Me.ImageSize.Height) Case ContentAlignment.MiddleLeft r = New Rectangle(8, Me.Height / 2 - Me.ImageSize.Height / 2, Me.ImageSize.Width, Me.ImageSize.Height) Case ContentAlignment.MiddleCenter r = New Rectangle(Me.Width / 2 - Me.ImageSize.Width / 2, Me.Height / 2 - Me.ImageSize.Height / 2, Me.ImageSize.Width, Me.ImageSize.Height) Case ContentAlignment.MiddleRight r = New Rectangle(Me.Width - 8 - Me.ImageSize.Width, Me.Height / 2 - Me.ImageSize.Height / 2, Me.ImageSize.Width, Me.ImageSize.Height) Case ContentAlignment.BottomLeft r = New Rectangle(8, Me.Height - 8 - Me.ImageSize.Height, Me.ImageSize.Width, Me.ImageSize.Height) Case ContentAlignment.BottomCenter r = New Rectangle(Me.Width / 2 - Me.ImageSize.Width / 2, Me.Height - 8 - Me.ImageSize.Height, Me.ImageSize.Width, Me.ImageSize.Height) Case ContentAlignment.BottomRight r = New Rectangle(Me.Width - 8 - Me.ImageSize.Width, Me.Height - 8 - Me.ImageSize.Height, Me.ImageSize.Width, Me.ImageSize.Height) End Select gr.DrawString("Header", New Font("MS SANS SERIF", 8.25, FontStyle.Regular, GraphicsUnit.Point, 1, True), Brushes.Black, 0, 0) 'gr.DrawImage(Me.Image, r) End Sub

As you can see @ gr.DrawString, I need to know how big the text is predicted to be so that I can size the control and center the text within the control.

Answer1:

Try <a href="http://msdn.microsoft.com/en-us/library/system.drawing.graphics.measurestring.aspx" rel="nofollow">MeasureString</a>()

gr.MeasureString("Header", New Font("MS SANS SERIF", 8.25, FontStyle.Regular, GraphicsUnit.Point, 1, True)).Width

Recommend

  • Finding touch location according to image in UIImageView
  • Change icon image size on SweetAlert
  • How to programmatically slice an image in 4 , 9 , 16 and 25 slices
  • How to make a UIImageView centered in a Scrollview when zoom out?
  • Android SurfaceView: Show Video after Images
  • Get byte[] from
  • IndexSizeError on drawImage on IE and Edge
  • maven-cobertura-plugion does not show the sources [closed]
  • How to convert text file automatically to graphviz dot file?
  • Permit array in params
  • How do I get rid of the Eclipse Find/Replace Bar
  • Getting coordinates of a component in java
  • Circle movement upon rectangle Collision
  • Titanium doesn't recognize Android SDK on Windows
  • c# winform DrawToBitmap offscreen
  • How to move an element within a structure, possibly with zippers?
  • What is this error in fortran and how to stop the program when it occurs?
  • pandas computation in each group
  • Why people use prototype in javascript when it is easy to inherit using apply () and call () methods
  • Why does it draw lines in the wrong place?
  • Implementing “partial void” in VB
  • Passing 1 of 3 values to a Sub
  • How to Cache Real-time Data?
  • How to attach a node.js readable stream to a Sendgrid email?
  • Very simple C++ DLL that can be called from .net
  • Jenkins: How To Build multiple projects from a TFS repository?
  • How do I fake an specific browser client when using Java's Net library?
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Hazelcast - OperationTimeoutException
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Web-crawler for facebook in python
  • Apache 2.4 - remove | delete | uninstall
  • RestKit - RKRequestDelegate does not exist
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Revoking OAuth Access Token Results in 404 Not Found
  • JTable with a ScrollPane misbehaving
  • unknown Exception android
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?