C#

[C#]문자를 Bitmap 파일로 변환하기

선영아 사랑해 2016. 1. 15. 16:17

문자를 이미지로 파일로 변경해야 할 경우 사용해보세요.


//파라미터는 문자, 폰트이름, 폰트사이즈, 폰트스타일

private Bitmap Convert_Text_to_Image(string txt, string fontname, int fontsize, FontStyle fontStyle)
{
    Bitmap bmp = new Bitmap(1, 1);


    Graphics graphics = Graphics.FromImage(bmp);


    Font font = new Font(fontname, fontsize, fontStyle);


    SizeF stringSize = graphics.MeasureString(txt, font);


    bmp = new Bitmap(bmp, (int)stringSize.Width, (int)stringSize.Height);


    graphics = Graphics.FromImage(bmp);


    graphics.DrawString(txt, font, Brushes.Black, 0, 0);


    font.Dispose();


    graphics.Flush();


    graphics.Dispose();


    return bmp;
}