views:

49

answers:

4

Given the following code. Is there any potential for the first DrawString method to draw in Arial rather than Times New Roman?

protected override void  OnPaint(PaintEventArgs pe)
{
    Font f = new Font("Times New Roman", this.TextSize);
    pe.Graphics.DrawString("Test 1", f, Brushes.Black, loc);
    f = new Font("Arial", this.TextSize);
    pe.Graphics.DrawString("Test 2", f, Brushes.Black, loc);
}

I have an issue where essentially this code is intermittently drawing the first string in the wrong font. I've changed the code to have two static font references now, but as I was unable to reproduce the code I can't be sure if it's fixed the problem or not.

*Note loc is a position that would be changed by the actual code, I've stripped out some code here to simplify

Here is the whole method with my fix in it. If you can't see anything wrong with it - I'll go blame some cosmic rays or something...

protected override void  OnPaint(PaintEventArgs pe)
{
       base.OnPaint(pe);
        if (bcf == null)
        {
            FontFamily[] families = pfc.Families;
            foreach (FontFamily ff in families)
            {
                if (ff.Name.Equals("Free 3 of 9"))
                {
                    bcf = ff;
                }
            }
        }
        if (bcf != null)
        {
            Font f = new Font(bcf, this.BarcodeSize);
            SizeF s = TextRenderer.MeasureText(barcodeValue, f);
            Rectangle r = pe.ClipRectangle;
            Point loc = new Point(0, 0);
            if (s.Width < r.Width)
            {
                loc.X = (int)Math.Ceiling((r.Width - s.Width) / 2);
            }
            pe.Graphics.DrawString(barcodeValue, f, Brushes.Black, loc);

            float barcodeBottom = s.Height + 5;

            Font fp = new Font("Arial", this.TextSize);
            s = TextRenderer.MeasureText(barcodeValue, fp);
            r = pe.ClipRectangle;
            loc = new Point(0, (int)Math.Ceiling(barcodeBottom));
            if (s.Width < r.Width)
            {
                loc.X = (int)Math.Ceiling((r.Width - s.Width) / 2);
            }
            if (s.Height + loc.Y > r.Height)
            {
                loc.Y = (int)Math.Ceiling(r.Height - (s.Height));
            }
            pe.Graphics.FillRectangle(Brushes.White, new Rectangle(loc, new Size((int)Math.Ceiling(s.Width), (int)Math.Ceiling(s.Height))));
            pe.Graphics.DrawString(barcodeValue, fp, Brushes.Black, loc);
        }
    }

The fixed code now looks like the following. Many fewer GDI calls now:

protected override void  OnPaint(PaintEventArgs pe)
    {
        base.OnPaint(pe);
        if (bcf != null)
        {
            Rectangle r = pe.ClipRectangle;
            Point loc = new Point(0, 0);
            if (barcodeDimensions.Width < r.Width)
            {
                loc.X = (int)Math.Ceiling((r.Width - barcodeDimensions.Width) / 2);
            }
            pe.Graphics.DrawString(barcodeValue, barcodeFont, Brushes.Black, loc);

            float barcodeBottom = barcodeDimensions.Height + 5;

            r = pe.ClipRectangle;
            loc = new Point(0, (int)Math.Ceiling(barcodeBottom));
            if (plaintextDimensions.Width < r.Width)
            {
                loc.X = (int)Math.Ceiling((r.Width - plaintextDimensions.Width) / 2);
            }
            if (plaintextDimensions.Height + loc.Y > r.Height)
            {
                loc.Y = (int)Math.Ceiling(r.Height - (plaintextDimensions.Height));
            }
            pe.Graphics.FillRectangle(Brushes.White, new Rectangle(loc, new Size((int)Math.Ceiling(plaintextDimensions.Width), (int)Math.Ceiling(plaintextDimensions.Height))));
            pe.Graphics.DrawString(barcodeValue, plaintextFont, Brushes.Black, loc);
        }
    }

If I was planning on making this even more optimal I'd put the rectangle measuring parts in an override of OnResize, but I think this will do for now...

+2  A: 

No, I can't see how that would possibly happen - it's not like the first call knows about the variable f - it only knows about its value at the time DrawString was called. The argument (a Font reference) is passed by value, not by reference.

The only way I could imagine this causing a problem is if the Graphics object remembers its "current" font (and resets it in the call to DrawString) but defers the actual drawing. That would have all kinds of nasty effects - I can't see it happening.

Basically, as far as the DrawString calls are concerned, it's as if you were using two different variables.

Jon Skeet
I figured as much, just not done much in GDI+ before and wasn't sure if there was something crazy happening here...
Matt Fellows
@Matt - It is quite possible.
Hans Passant
A: 

Skeet covered the base case. I've done a lot of GDI+ and I've never seen the behavior you describe. Most likely it is due to some other effect not shown by this code.

Ron Warholic
A: 

Can't see any problems with the code provided. Is it possible in your full code that the drawing locations are incorrect?

Are you 100% sure that the string value that is being rendered in Arial could only possibly be rendered by the call to DrawString using the TNR font?

PirateKitten
Aye - the only thing more complicated in the actual code is that the first font comes from a PrivateFontCollection. I iterate through the collection looking for a FontFamily with a particular name then render the text in a particular location.... I'll edit the question and put the whole method in there as well as the simple version just in case....
Matt Fellows
+2  A: 

Yes, strange things start to happen when your program is close to consuming 10,000 GDI handles. It almost certainly affects the Windows font mapper without necessarily throwing an exception. Your program is playing Russian roulette with this potential problem because you are not calling Dispose() on the fonts you use. If the garbage collector doesn't run frequently enough then you may well get that gun to go off. You need to write it like this:

using (Font fp = new Font("Arial", this.TextSize)) {
    // etc..
}

Also note another bug in your code, you are using TextRenderer.MeasureText but drawing with Graphics.DrawString. The measurement is not identical. You must use Graphics.MeasureString.

Hans Passant
I don't think it should be done in the OnPaint method anyway. I'm in the process of refactoring the whole thing to only set the fonts, sizes and location in the setter for the text which gets rendered.
Matt Fellows