473,396 Members | 1,655 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,396 software developers and data experts.

gdi+ and line break

Hi, I'd like to render colorful text with word wrap on screen (kind of
inline <fonttag to change text color in html), but run into the line
break problem.

Graphics provides several DrawString methods with line break features,
but I can't make them draw different colors within one string, And if
I split the string into several part, so I can specify their colors,
line breaking won't work.

So, I have to break my string to lines before I pass it/them to
DrawString. This is something I don't know (yet) how to do efficiently
in c#.

I can create many sub strings (with length 1, 2, 3, ...) to
MeasureString them, but this leads to lots of gabage objects, which I
think I should avoid in a OnPaint event handler;

And I can use MeasureCharacterRanges to every chars in the string, so
I will have every char's width. but I found I can only process 32
chars a time because if I passed more CharacterRanges to
StringFormat.SetMeasurableCharacterRanges, there would be an
OverflowException. Since there is a limit number, I think this may be
more unefficient than making lots of strings.

I think there should be a way to do this, though I havn't found it
right now. I looked around for a HTML renderer example, but none of
them is done in C# or .net without a Browser control.

Any help is appreciated, Thanks!
Dec 12 '07 #1
4 5778
You mentioned that you should avoid creating a lot of objects in the
OnPaint handler. I don't think that this is an issue here. Granted, you
might trigger memory pressure in the OnPaint handler, but simply creating
the objects isn't going to trigger a GC. A GC is going to happen when it
happens. Also, creating all of those strings and not holding references to
them is going to make the cleanup of those strings rather quick (it's not
going to survive past one generation).

Are you using .NET 3.0 by chance? If so, have you considered using WPF?
It will make what you are trying to do much, MUCH easier and it might be
worth the upgrade if you are already using .NET 2.0 (as it is just extra
libraries, no CLR changes).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"deerchao" <de******@gmail.comwrote in message
news:95**********************************@s8g2000p rg.googlegroups.com...
Hi, I'd like to render colorful text with word wrap on screen (kind of
inline <fonttag to change text color in html), but run into the line
break problem.

Graphics provides several DrawString methods with line break features,
but I can't make them draw different colors within one string, And if
I split the string into several part, so I can specify their colors,
line breaking won't work.

So, I have to break my string to lines before I pass it/them to
DrawString. This is something I don't know (yet) how to do efficiently
in c#.

I can create many sub strings (with length 1, 2, 3, ...) to
MeasureString them, but this leads to lots of gabage objects, which I
think I should avoid in a OnPaint event handler;

And I can use MeasureCharacterRanges to every chars in the string, so
I will have every char's width. but I found I can only process 32
chars a time because if I passed more CharacterRanges to
StringFormat.SetMeasurableCharacterRanges, there would be an
OverflowException. Since there is a limit number, I think this may be
more unefficient than making lots of strings.

I think there should be a way to do this, though I havn't found it
right now. I looked around for a HTML renderer example, but none of
them is done in C# or .net without a Browser control.

Any help is appreciated, Thanks!

Dec 12 '07 #2
Thanks!

What you said made me feel much better. I'll use the substring way to
break text into lines.

One of my friends showed me how to do this in WPF with TextBlock, it's
dead easy, made me want to kill myself. Anyway, I can't just switch to
WPF from WinForms right now, I'm not ready (know little about WPF),
and I don't think my users are ready, either. May be after one or more
years, I'll turn to WPF.

Thanks again!

On Dec 13, 1:13 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
You mentioned that you should avoid creating a lot of objects in the
OnPaint handler. I don't think that this is an issue here. Granted, you
might trigger memory pressure in the OnPaint handler, but simply creating
the objects isn't going to trigger a GC. A GC is going to happen when it
happens. Also, creating all of those strings and not holding references to
them is going to make the cleanup of those strings rather quick (it's not
going to survive past one generation).

Are you using .NET 3.0 by chance? If so, have you considered using WPF?
It will make what you are trying to do much, MUCH easier and it might be
worth the upgrade if you are already using .NET 2.0 (as it is just extra
libraries, no CLR changes).

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"deerchao" <deerc...@gmail.comwrote in message

news:95**********************************@s8g2000p rg.googlegroups.com...
Hi, I'd like to render colorful text with word wrap on screen (kind of
inline <fonttag to change text color in html), but run into the line
break problem.
Graphics provides several DrawString methods with line break features,
but I can't make them draw different colors within one string, And if
I split the string into several part, so I can specify their colors,
line breaking won't work.
So, I have to break my string to lines before I pass it/them to
DrawString. This is something I don't know (yet) how to do efficiently
in c#.
I can create many sub strings (with length 1, 2, 3, ...) to
MeasureString them, but this leads to lots of gabage objects, which I
think I should avoid in a OnPaint event handler;
And I can use MeasureCharacterRanges to every chars in the string, so
I will have every char's width. but I found I can only process 32
chars a time because if I passed more CharacterRanges to
StringFormat.SetMeasurableCharacterRanges, there would be an
OverflowException. Since there is a limit number, I think this may be
more unefficient than making lots of strings.
I think there should be a way to do this, though I havn't found it
right now. I looked around for a HTML renderer example, but none of
them is done in C# or .net without a Browser control.
Any help is appreciated, Thanks!
Dec 13 '07 #3
Just a piece of advice, you might want to take a look at it sooner than
later. It's easier to learn the basics now and keep up with them as you
move along.

Granted, there is a cost, but if doing what you are doing now was done
so easy in WPF, imagine what other things could be done more easily for you.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"deerchao" <de******@gmail.comwrote in message
news:5d**********************************@b1g2000p ra.googlegroups.com...
Thanks!

What you said made me feel much better. I'll use the substring way to
break text into lines.

One of my friends showed me how to do this in WPF with TextBlock, it's
dead easy, made me want to kill myself. Anyway, I can't just switch to
WPF from WinForms right now, I'm not ready (know little about WPF),
and I don't think my users are ready, either. May be after one or more
years, I'll turn to WPF.

Thanks again!

On Dec 13, 1:13 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
> You mentioned that you should avoid creating a lot of objects in the
OnPaint handler. I don't think that this is an issue here. Granted, you
might trigger memory pressure in the OnPaint handler, but simply creating
the objects isn't going to trigger a GC. A GC is going to happen when it
happens. Also, creating all of those strings and not holding references
to
them is going to make the cleanup of those strings rather quick (it's not
going to survive past one generation).

Are you using .NET 3.0 by chance? If so, have you considered using
WPF?
It will make what you are trying to do much, MUCH easier and it might be
worth the upgrade if you are already using .NET 2.0 (as it is just extra
libraries, no CLR changes).

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"deerchao" <deerc...@gmail.comwrote in message

news:95**********************************@s8g2000 prg.googlegroups.com...
Hi, I'd like to render colorful text with word wrap on screen (kind of
inline <fonttag to change text color in html), but run into the line
break problem.
Graphics provides several DrawString methods with line break features,
but I can't make them draw different colors within one string, And if
I split the string into several part, so I can specify their colors,
line breaking won't work.
So, I have to break my string to lines before I pass it/them to
DrawString. This is something I don't know (yet) how to do efficiently
in c#.
I can create many sub strings (with length 1, 2, 3, ...) to
MeasureString them, but this leads to lots of gabage objects, which I
think I should avoid in a OnPaint event handler;
And I can use MeasureCharacterRanges to every chars in the string, so
I will have every char's width. but I found I can only process 32
chars a time because if I passed more CharacterRanges to
StringFormat.SetMeasurableCharacterRanges, there would be an
OverflowException. Since there is a limit number, I think this may be
more unefficient than making lots of strings.
I think there should be a way to do this, though I havn't found it
right now. I looked around for a HTML renderer example, but none of
them is done in C# or .net without a Browser control.
Any help is appreciated, Thanks!
Dec 14 '07 #4
On Dec 12, 9:17 am, deerchao <deerc...@gmail.comwrote:
Hi, I'd like to render colorful text with word wrap on screen (kind of
inline <fonttag to change text color in html), but run into the line
break problem.
[snip]
I think there should be a way to do this, though I havn't found it
right now. I looked around for a HTML renderer example, but none of
them is done in C# or .net without a Browser control.
Can you use a browser control?

(The problem with .Net 3.0 is that it won't run on Win2K - and
certainly our customers won't accept that for several years ... it's
not long since we weaned them off NT4 + Win98)
Dec 14 '07 #5

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

Similar topics

6
by: James dean | last post by:
I have heard that the video drivers in GDI+ are a big performance issue. But is this only an issue with something like Games Programming i think...is this wrong?. What about a drawing application...
1
by: James dean | last post by:
Could someone explain how this works. I think the graphics card is used to do blitting and drawing shapes like rectangles. How does it draw using the Graphics card on the PC and why is this feature...
5
by: Pohihihi | last post by:
I am getting a generic GDI+ error on following code. Basically it is trying to capture screen image and save in a file. Is there a way to find more details on this error? Thanks for the help. ...
7
by: news | last post by:
This may be a stupid question, but if I don't ask I'll never know ;) Ok, here it goes.... I am writing an application that renders an image in one picturebox and a graph in another. The image...
2
by: Zanna | last post by:
Hi all! I'm in difficulty with this: I need to know the height of a text line that is written with Graphics.DrawString(). In theory I need just to get the Graphics.MeasureString() result. But...
2
by: Alphonse Giambrone | last post by:
I am currently reading 'Programming The Web with Visual Basic .NET' and have so far found it to be excellent. Downloaded all the code from Apress and working in chapter 4, I get the error shown...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
0
by: cmbardon | last post by:
I'm trying to create a form with some rounded controls, and I'm having some problems with the rounding not working correctly. I've tried a couple of different methods for creating the graphics...
2
by: user | last post by:
Hi all... i am trying to print an image using GDI - but the only thing i get is a black rectangle. Does anybody know what is wrong, or how else to get the bitmap on the printer (GDI+ is not...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...

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.