Properly Sizing a Bitmap

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smgrisw
    New Member
    • Feb 2009
    • 2

    Properly Sizing a Bitmap

    All,

    I am trying to create a bitmap dynamically based on a selection of text. Code is below:
    Code:
    bitMapImage = new
                    System.Drawing.Bitmap(90,50);
    
          Graphics graphicImage = Graphics.FromImage(bitMapImage);
    
                graphicImage.Clear(Color.White);
                SolidBrush drawBrush = new SolidBrush(Color.Red);
    
                graphicImage.DrawString(comboStampText.Text +  DateTime.Now.ToShortDateString(),
                   new Font("Arial", 18, FontStyle.Regular),
                   drawBrush, new Point(5, 5));
    Everthing works except I am having difficulty sizing the bitmap correctly based on the length of the text. Is there a way that this can be accomplished?

    Regards,

    Scott
    Last edited by pbmods; Feb 5 '09, 02:50 AM. Reason: Added CODE tags.
  • vekipeki
    Recognized Expert New Member
    • Nov 2007
    • 229

    #2
    You can use System.Graphics .MeasureString( text, font) to get the size of your string for that graphics instance - but it also has a couple of things you should know before using it, check this link for an example: http://www.codeproject.com/KB/GDI-pl...urestring.aspx

    There is also the System.Windows. Forms.TextRende rer class, which behaves a bit different when rendering (it uses plain GDI instead of GDI+). In some cases it is a better solution (for example, when rendering semi-transparent text to a bitmap, it does a better job with anti-aliasing).

    Comment

    Working...