ArgumentException in DrawString

I have some code which worked fine in framework 1.1 but is giving me an ArgumentException in 2.0.  The code follows:

using
(Font fntDraw = new Font("Arial", 16, FontStyle.Regular))
{
   Rectangle rcLayout = new Rectangle(0, 0, _cpixCell, _cpixCell);
   
StringFormat sfmt = new StringFormat();
   sfmt.Alignment =
StringAlignment.Center;
   sfmt.LineAlignment =
StringAlignment.Center;
   g.DrawString(
"F", fntDraw, br, rcLayout, sfmt);
}

The DrawString call generates the exception.  I can draw pretty much anything else into this Graphics object (which is attached to an internal bitmap) and I could draw the "F" in 1.1 but in 2.0 this blows up.  Any ideas

Thanks!

Darrell Plank



Answer this question

ArgumentException in DrawString

  • Sachin Sinha MSFT

    I think you've nailed it.  Pretty obscure - not exactly writ large in the documentation and confusing when it arises but I appreciate the info.


  • David Mc Quillan

    Does yours work with a regular Rectangle rather than a RectangleF

    I was hopeful that this would fix it originally but it doesn't seem to work in my project.  I wondered if there were some way the font didn't really exist on my machine (I switched machines as well as platforms when I went from 1.1 to 2.0) but the font object comes back with no complaint and I can't imagine having any problem with Arial as a font.


  • markp5511

    It's just a "new SolidBrush(Color.Black)".  It's a fine brush.  For instance, if I replace the drawing code with:

    g.FillRectangle(br, new Rectangle(0, 0, _cpixCell, _cpixCell));

    then I get the expected solid black square.


  • awesj

    Okay, I made a test case and found out the nature of the problem.  Here's the test case:

    Bitmap bmp = new Bitmap(30, 30);
    using (Graphics g = Graphics.FromImage(bmp))
    {
       
    #if false
       g.CompositingMode = CompositingMode.SourceCopy;
       g.SmoothingMode = SmoothingMode.AntiAlias;
       g.PixelOffsetMode = PixelOffsetMode.HighQuality;
       #endif
       using (Font fntDraw = new Font("Arial", 16, FontStyle.Regular))
       {
          g.DrawString("F", fntDraw, new SolidBrush(Color.Black), new PointF(0f, 0f));
       }
    }

    If the stuff in the conditional is put in then we get the error in 2.0 but not in 1.1.  If it's excluded then we work okay on either platform.


  • David Holcomb

  • Marfig

  • Pravin Indurkar

    Well, it seems like that *ought* to be the answer, but I've tried putting in:

    RectangleF rcLayout = new RectangleF(0, 0, 10.0f, 10.0f);

    and the problem persists.  Sigh.

    It is interesting, though, if it was possible to use a Rectangle in 1.1 but not in 2.0.  I would have assumed that it was just coercing the Rectangle into a RectangleF which is a performance hit but ought to work in 2.0 if it worked in 1.1.

    Strange.


  • Vladimir Savinov

    Yeah, I'd already tried that but it didn't work.  I probably shouldn't have changed it back.
  • Stubey

    Great to have the sample code.  This error happens because CompositingMode.SourceCopy is not allowed when ClearType is ON and it occurs in both Everett and Whidbey.  Probably you run your Everett test in a system with ClearType off.

  • Kamran100

    Is the exception you are getting an ArgumentNullException or just ArgumentException Is it possible for you to add the call stack for the failure

    I have tried your code and cannot reproduce the problem.  Can you report a bug on this adding detail steps to reproduce the issue http://lab.msdn.microsoft.com/productfeedback/Default.aspx

    thanks,



  • sonali1754

    With a RectangleF I can get it working here.

  • rsb_mda

    ArgumentException mean there is something wrong with one of your arguments.. What does argument "br" mean

  • ArgumentException in DrawString