473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Graphics.DrawString question

Howdy Folks

I have a display where the Graphics.DrawString function is called to
display something. Since the text seems to be larger than its
bounding rectangle, the call basically splits the string into multiple
lines.

Is there a way to find out into how many lines the string was split?

I am asking because this string is being displayed inside an owner
drawn list view. I need to make the height of the row big enough to
accommodate this multi-line string. But I can't seem to reliably
determine how to do that. I thought:

Graphics graphicsObject = ...;
Font someFont = ...;
StringFormat sf = ...;
Rectangle rect = ...;

string s = "This is some string";
SizeF measuredSize = graphicsObject.MeasureString(s, someFont, new
Point(2, 2), sf);

graphicsObject.DrawString(s, someFont, someBrush, rect, sf);

int lines = (int)(measuredSize.Width / rect.Width) + 1; // add 1 to be
on the safe side

The above line doesn't seem to give the correct answer.

Any ideas?

Oct 7 '08 #1
6 2658
On Tue, 07 Oct 2008 12:31:21 -0700, Dilip <rd*****@lycos.comwrote:
[...]
SizeF measuredSize = graphicsObject.MeasureString(s, someFont, new
Point(2, 2), sf);

graphicsObject.DrawString(s, someFont, someBrush, rect, sf);

int lines = (int)(measuredSize.Width / rect.Width) + 1; // add 1 to be
on the safe side

The above line doesn't seem to give the correct answer.
It's hard to know for sure when you don't post the actual code you're
using. But assuming the above is reasonably close to what you're doing,
then the most obvious problem is that you're not calling a MeasureString()
overload that allows you to specify the width of the rendered text.
Because of that, MeasureString() has no way to take into account any
automatic wrapping that will occur when you call DrawString().

Pete
Oct 7 '08 #2
On Oct 7, 3:01*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Tue, 07 Oct 2008 12:31:21 -0700, Dilip <rdil...@lycos.comwrote:
[...]
SizeF measuredSize = graphicsObject.MeasureString(s, someFont, new
Point(2, 2), sf);
graphicsObject.DrawString(s, someFont, someBrush, rect, sf);
int lines = (int)(measuredSize.Width / rect.Width) + 1; // add 1 to be
on the safe side
The above line doesn't seem to give the correct answer.

It's hard to know for sure when you don't post the actual code you're *
using. *But assuming the above is reasonably close to what you're doing, *
then the most obvious problem is that you're not calling a MeasureString() *
overload that allows you to specify the width of the rendered text. *
Because of that, MeasureString() has no way to take into account any *
automatic wrapping that will occur when you call DrawString().

Pete
Peter
Apologies. The codebase is quite hairy and I wanted to post something
minimal. Do you mean I should use this overload of MeasureString?

public SizeF MeasureString(string text, Font font, SizeF layoutArea,
StringFormat stringFormat,
out int charactersFitted, out
int linesFilled);

IOW, the code I posted:

SizeF measuredSize = graphicsObject.MeasureString(s, someFont, new
Point(2, 2), sf);
graphicsObject.DrawString(s, someFont, someBrush, rect, sf);

becomes:

Rectangle rect = ...; // bounding rectangle where string is to be
displayed
SizeF layoutRect = new SizeF(rect.Width, rect.Height);
int charsFitted, linesFilled;
SizeF measuredSize = graphicsObject.MeasureString(s, someFont,
layoutRect, sf, out charsFitted,

out linesFilled);

am I right?
Oct 7 '08 #3
Dilip:
Try with this code segment:

Graphics graphicsObject = ...;
Font someFont = ...;
StringFormat sf = ...;
Rectangle rect = ...;

string s = "This is some string";
SizeF measuredSize = graphicsObject.MeasureString(s, someFont,
new Point(2, 2), sf);

int lines = (int)(measuredSize.Height /
someFont.GetHeight(graphicsObject));

Gustavo A. Cantero
CEO - Scientia® Soluciones Informáticas
MCP - MCSD - MCTS
http://www.scientia.com.ar
http://www.programandoamedianoche.com
http://foro.scientia.com.ar

-----Mensaje original-----
De: Dilip [mailto:rd*****@lycos.com]
Expuesto a las: Martes, 07 de Octubre de 2008 04:31 p.m.
Expuesto en: microsoft.public.dotnet.languages.csharp
Conversación: Graphics.DrawString question
Asunto: Graphics.DrawString question

Howdy Folks

I have a display where the Graphics.DrawString function is called to
display something. Since the text seems to be larger than its
bounding rectangle, the call basically splits the string into multiple
lines.

Is there a way to find out into how many lines the string was split?

I am asking because this string is being displayed inside an owner
drawn list view. I need to make the height of the row big enough to
accommodate this multi-line string. But I can't seem to reliably
determine how to do that. I thought:

Graphics graphicsObject = ...;
Font someFont = ...;
StringFormat sf = ...;
Rectangle rect = ...;

string s = "This is some string";
SizeF measuredSize = graphicsObject.MeasureString(s, someFont, new
Point(2, 2), sf);

graphicsObject.DrawString(s, someFont, someBrush, rect, sf);

int lines = (int)(measuredSize.Width / rect.Width) + 1; // add 1 to be
on the safe side

The above line doesn't seem to give the correct answer.

Any ideas?

Oct 7 '08 #4
On Tue, 07 Oct 2008 13:21:21 -0700, Dilip <rd*****@lycos.comwrote:
Apologies. The codebase is quite hairy and I wanted to post something
minimal.
No one would expect you to post the entire program you're writing. But
you should expect to have to post a concise-but-complete code sample that
reliably demonstrates the issue. That often involves taking the code
you're working with and paring it down into a short program that includes
only the minimal code needed to demonstrate the issue at hand.
Do you mean I should use this overload of MeasureString?

public SizeF MeasureString(string text, Font font, SizeF layoutArea,
StringFormat stringFormat,
out int charactersFitted, out
int linesFilled);
Well, you don't necessarily need to use that overload, unless you actually
need the "out" values it returns. But yes, you need to use one of the
overloads that has the "layoutArea" argument.

That's assuming my interpretation of the problem and guess at the answer
is correct. Without a concise-but-complete code sample from you, there's
no way to know for sure. You'll just have to try it yourself and see.

Pete
Oct 7 '08 #5
On Oct 7, 3:20*pm, "Gustavo Cantero" <g.cant...@scientia.com.ar>
wrote:
Dilip:
* * * * Try with this code segment:

* * * * Graphics graphicsObject = ...;
* * * * Font someFont = ...;
* * * * StringFormat sf = ...;
* * * * Rectangle rect = ...;

* * * * string s = "This is some string";
* * * * SizeF measuredSize = graphicsObject.MeasureString(s, someFont,
new Point(2, 2), sf);

* * * * int lines = (int)(measuredSize.Height /
someFont.GetHeight(graphicsObject));

Gustavo A. Cantero
CEO - Scientia® Soluciones Informáticas
MCP - MCSD - MCTShttp://www.scientia.com.arhttp://www.programandoamedianoche.comhttp://foro.scientia.com.ar
Gustavo

Thank you very much. Much appreciated. I actually noticed your post
a tad late so I didn't try your approach yet. My reply to Peter
elsethread also solved the problem. Do take a look and let me know if
you find anything wrong with it.

Cheers

Oct 7 '08 #6
On Oct 7, 3:28*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
That's assuming my interpretation of the problem and guess at the answer *
is correct. *
Your interpretation and your guess were both right. I am now able to
determine the # of lines the string is being split into if it doesn't
fit inside the layout area.

Thanks!
Oct 7 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: twostepted | last post by:
Hello, This is a bit of a long post (sorry) but here is the jist of it: I've developed a Windows forms User Control marquee (auto-scrolling text like a readerboard). I'm trying to optimize it...
5
by: Charles A. Lackman | last post by:
Hello, I have created a complete PrintDocument and need to create an image from it. How is this done? e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality...
7
by: Michael Galvin | last post by:
I am trying to use Python to send to the printer a calender filled with a mix of text and simple graphics. I want to draw on the printed page something like a table with 6 rows and 7 columns to...
7
by: Peter Row | last post by:
Hi, I've started work on my own control some parts of which use standard controls, others I need to draw on my controls surface to get the display output I require, however.... I seem to be...
7
by: billsahiker | last post by:
I am using the Microsoft Press training kit for the Framework 2.0 exam. It has the following example for putting text into a font and drawing it. I created a new Windows Application. But where do...
2
by: Tony Johansson | last post by:
Hello! If I use the DrawString below with object of StringFormat as the last object it works good. If I instead remove object StringFormat below as the last object of DrawString I get some rows...
6
vekipeki
by: vekipeki | last post by:
I am having a problem with basic drawing of unicode characters in Windows 2000 and XP. I have written a simplest possible C# WinForms program to test it (just create a new Windows Forms C#...
3
by: arunman | last post by:
Hi, Im writing a C# .Net application to print in zebra 2030 thermal receipt printer (Uses 80 mm wide paper). I'm using the windows Graphics object for printing. string text = "Line1\n ...
10
by: anuking | last post by:
Hi, I made a tool that compares the texts from 2 richtextboxes and then marks the characters that are different in red. An option needs me to overlap these 2 text data to show the exact...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.