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 so that it'll
display very large strings with out comsuming so many resources. That
is, as the size of the display string increases, the marquee uses
additional CPU cycles.
I've considered two possible solutions.
The easiest solution would be if there were a way to make the GDI+
Graphics.DrawString handle large strings gracefully. I'm not a very
experienced graphics programmer myself. I've been looking at
providing a Clip area (Graphics.Clip) Region. However, this didn't
seem to solve the performance problem. I've also tried providing a
rectangular bounds (RectangleF) to the DrawString method. However,
this hasn't had any impact on performance either. You can see my
current OnPaint() method below. Some of these past ideas are commented
out therein.
I've considered splitting up the DisplayText string and only displaying
one portion at a time. However, this solution introduces some other
difficulties. Mainly, keeping track of the last StringWidth/Length and
calculating the correct x, y coords as the next string is loaded.
Here is the method. I haven't shown how I provide the clip area as
that is done in at Init().
protected override void OnPaint(PaintEventArgs e)
{
_gfx = e.Graphics;
// only load the brushes if new color
if(bNewColor)
LoadBrushes();
// only build the graphic string if new string
if(bNewText)
BuildStringGraphic(e.Graphics);
// draw BG
_gfx.FillRectangle(BrushBG, 0, 0, Width, Height);
// draw the string at the
_gfx.DrawString(_sbDisplayCurrent.ToString(), _font,
BrushFG, x, y);
// draw string inside of rectangle
//_gfx.DrawString(_sbDisplayCurrent.ToString(), _font,
// BrushFG,
// new RectangleF(new PointF(x, y), new SizeF
// (this.Width, this.Height)), sf);
}