Click here to Skip to main content
15,885,669 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
private Bitmap CreateBitmapImage(string sImageText)
{
    Bitmap objBmpImage = new Bitmap(2, 2);

    int intWidth = 0;
    int intHeight = 0;

    // Create the Font object for the image text drawing.
    System.Drawing.Font objFont = new System.Drawing.Font("Arial",10, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);

    // Create a graphics object to measure the text's width and height.
    Graphics objGraphics = Graphics.FromImage(objBmpImage);

    // This is where the bitmap size is determined.
    intWidth = (int)objGraphics.MeasureString(sImageText, objFont).Width;
    intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height;

    // Create the bmpImage again with the correct size for the text and font.
    objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight) );


    // Add the colors to the new bitmap.
    objGraphics = Graphics.FromImage(objBmpImage);

    // Set Background color

    objGraphics.Clear(System.Drawing.Color.White);
    objGraphics.SmoothingMode = SmoothingMode.HighQuality;



    objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;
    objGraphics.DrawString(sImageText, objFont, new SolidBrush(System.Drawing.Color.Black), 0, 0, StringFormat.GenericDefault);

    objGraphics.Flush();

    return (objBmpImage);
}


I am using this code but image with text does not clear.


[edit]SHOUTING removed, Code block added - OriginalGriff[/edit]
Posted
Updated 21-May-12 3:32am
v3
Comments
OriginalGriff 21-May-12 9:29am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
pradeep rasara 22-May-12 9:36am    
its also not working with antialias.

1 solution

Please read here: How to: Use Antialiasing with Text[^].
C#
private Bitmap CreateBitmapImage(string sImageText)
{
    Bitmap objBmpImage = new Bitmap(2, 2);

    int intWidth = 0;
    int intHeight = 0;

    // Create the Font object for the image text drawing.
    System.Drawing.Font objFont = new System.Drawing.Font("Arial",10, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);

    // Create a graphics object to measure the text's width and height.
    Graphics objGraphics = Graphics.FromImage(objBmpImage);

    // This is where the bitmap size is determined.
    intWidth = (int)objGraphics.MeasureString(sImageText, objFont).Width;
    intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height;

    // Create the bmpImage again with the correct size for the text and font.
    objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight) );


    // Add the colors to the new bitmap.
    objGraphics = Graphics.FromImage(objBmpImage);

    // Set Background color

    objGraphics.Clear(System.Drawing.Color.White);
    objGraphics.SmoothingMode = SmoothingMode.HighQuality;



    objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; //  <-- This is the correct value to use. ClearTypeGridFit is better yet!
    objGraphics.DrawString(sImageText, objFont, new SolidBrush(System.Drawing.Color.Black), 0, 0, StringFormat.GenericDefault);

    objGraphics.Flush();

    return (objBmpImage);
}
 
Share this answer
 
v2
Comments
Wonde Tadesse 21-May-12 20:21pm    
5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900