如何在C#中使用文本制作高质量图像 [英] How to make high qaulity image with text in C#

查看:58
本文介绍了如何在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);
}



我正在使用此代码,但带有文字的图像无法清除.


[edit]删除了喊话,添加了代码块-OriginalGriff [/edit]



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


[edit]SHOUTING removed, Code block added - OriginalGriff[/edit]

推荐答案

请在此处阅读: ^ ].
Please read here: How to: Use Antialiasing with Text[^].
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);
}


这篇关于如何在C#中使用文本制作高质量图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆