I am trying to optimize the following code:
private void Screen_Paint(object sender, PaintEventArgs e)
{
Graphics bmpGraphics = Graphics.FromImage(_screenBitmap);
int xLoc = 0;
int yLoc = 0;
bmpGraphics.Clear(Color.Black);
_resetInvoked = true;
for (int i = 0; i < 4000; i += 2)
{
var bgBit = (byte) ((_screenMemory[i + 1]) & 112);
var fgBit = (byte)((_screenMemory[i + 1]) & 15);
SolidBrush bgBrush = _backgroundBrushesArray[bgBit/16];
SolidBrush fgBrush = _foregroundBrushesArray[fgBit];
if (((xLoc % 640) == 0) && (xLoc != 0))
{
yLoc += 12;
xLoc = 0;
}
if (_resetInvoked)
{
string s = System.Text.Encoding.ASCII.GetString(_screenMemory, i, 1);
var pf = new PointF(xLoc, yLoc);
bmpGraphics.FillRectangle(bgBrush, xLoc + 2, yLoc + 3, 8f, 11f);
bmpGraphics.DrawString(s, _renderFont, fgBrush, pf);
}
xLoc += 8;
}
_resetInvoked = false;
e.Graphics.DrawImage(_screenBitmap, new Point(0, 0));
bmpGraphics.Dispose();
}
This is a Winforms User Control that mimics a console like screen. Its working just fine, but I'd like to speed it up. Running it through Telerik's JustCode performance monitor, the slowest performance seems to come from the FillRectangle
and DrawString
methods. Is there anyway to speed this up?
_resetInvoked
to true in the beginning and then set to false, and why isif (_resetInvoked)
in there if it's always true? – janos♦ Dec 1 '14 at 20:11_resetInvoked
is a flag used to determine, basicially, if thePaint
event is currently already "painting". I didn't want thePaint
event to execute if there is already a painting going on. – Icemanind Dec 1 '14 at 21:20_resetInvoked
is a strange name for that, unless it is only set during reset events. I would recommend something more like_busy
since that's what you seem to be using it for. – Nick Udell Dec 2 '14 at 9:44