DrawString in .NET

Hi,

I am trying to use the DrawString function so that it does not put a space at the
heading and trailing of a string

I have tried MeasuretrailingSpaces but that doesn't seem to work.

I want the string to start exactly at my x coordinate.

Do i have to set the StringFormat to achieve this

rotsey


Answer this question

DrawString in .NET

  • David Hary

    Thanks for post Daniel

    Did I say i am doing this in ASP.NET

    I could not find that method on the

    system.web.ui.webcontrols.label

    control

    rotsey


  • Robert Q

    Thanks Chris but that is not the issue.

    I have a page with a textarea and a label control and some controls to select the font style.

    The user types in the textarea and the label control is poopulated with the text and the font style that has been selected.

    A button allows the creation of a bitmap using the text and font style.

    The problem is the width of the text is restricted so that if any text is longer then the label it wraps.

    When i goto create the bitmap i use DrawString function but it seems to put some space at the beginning of the text and as the label control does not it means that what the user types in is not always going to be what they get in the bitmap because of the space.

    So you see the problem is with DrawString somehow

    I have done some searching on this and it seems to the GDI function that is causing the problem, but how to fix I dont know


    rotsey




  • cubeberg

    Hi,

    Try to trim your string first before you use DrawString:

    string myString = "   string   ";
    myString = myString.Trim();

    // Draw your string here...



    Hope this helps,

    -chris

  • Martin Kristensen

    If your label already shows the image you want, you can use the DrawToBitmap method:



    Bitmap bmp = new Bitmap(label1.Width, label1.Height);
    label1.DrawToBitmap(bmp, new Rectangle(0, 0, label1.Width, label1.Height));
    bmp.SaveAs(filename, ImageFormat.Bmp);

     


  • DVINH

    This method is only defined for the WinForms controls.
    You might want to ask your question on forums.asp.net.



  • DrawString in .NET