■ 팬시 텍스트를 그리는 방법을 보여준다.
▶ FontHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
using System; using System.Drawing; using System.Drawing.Text; using System.Drawing.Drawing2D; namespace TestProject { /// <summary> /// 폰트 헬퍼 /// </summary> public class FontHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// BLUR 크기 /// </summary> private static readonly int BLUR_AMOUNT = 6; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 팬시 텍스트 이미지 구하기 - GetFancyTextImage(text, font, foregroundColor, backgroundColor) /// <summary> /// 팬시 텍스트 이미지 구하기 /// </summary> /// <param name="text">텍스트</param> /// <param name="font">폰트</param> /// <param name="foregroundColor">전경 색상</param> /// <param name="backgroundColor">배경 색상</param> /// <returns>팬시 텍스트 이미지</returns> public static Bitmap GetFancyTextImage(string text, Font font, Color foregroundColor, Color backgroundColor) { Bitmap targetBitmap = null; using(Graphics desktopGraphics = Graphics.FromHwnd(IntPtr.Zero)) { SizeF textSize = desktopGraphics.MeasureString(text, font); using(Bitmap textBitmap = new Bitmap((int)textSize.Width,(int)textSize.Height)) { using(Graphics textBitmapGraphics = Graphics.FromImage(textBitmap)) { using(SolidBrush backgroundBrush = new SolidBrush(Color.FromArgb(16, backgroundColor.R, backgroundColor.G, backgroundColor.B))) { using(SolidBrush foregroundBrush = new SolidBrush(foregroundColor)) { textBitmapGraphics.SmoothingMode = SmoothingMode.HighQuality; textBitmapGraphics.InterpolationMode = InterpolationMode.HighQualityBilinear; textBitmapGraphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; textBitmapGraphics.DrawString(text, font, backgroundBrush, 0, 0); targetBitmap = new Bitmap(textBitmap.Width + BLUR_AMOUNT, textBitmap.Height + BLUR_AMOUNT); using(Graphics targetBitmapGraphics = Graphics.FromImage(targetBitmap)) { targetBitmapGraphics.SmoothingMode = SmoothingMode.HighQuality; targetBitmapGraphics.InterpolationMode = InterpolationMode.HighQualityBilinear; targetBitmapGraphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; for(int x = 0; x <= BLUR_AMOUNT; x++) { for(int y = 0; y <= BLUR_AMOUNT; y++) { targetBitmapGraphics.DrawImageUnscaled(textBitmap, x, y); } } targetBitmapGraphics.DrawString(text, font, foregroundBrush, BLUR_AMOUNT/2, BLUR_AMOUNT/2); } } } } } } return targetBitmap; } #endregion } } |
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
using System.Drawing; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 텍스트 비트맵 /// </summary> private Bitmap textBitmap; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); using(Font font = new Font("Arial", 30, FontStyle.Bold)) { this.textBitmap = FontHelper.GetFancyTextImage("Hello World!", font, Color.Red, Color.Blue); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 페인트시 처리하기 - OnPaint(e) /// <summary> /// 페인트시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnPaint(PaintEventArgs e) { base.OnPaint (e); e.Graphics.DrawImageUnscaled(textBitmap, 50, 50); } #endregion } } |