■ 도트 폰트를 생성하는 방법을 보여준다.
▶ 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
using System.Collections.Generic; using System.Globalization; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Imaging; namespace TestProject { /// <summary> /// 폰트 헬퍼 /// </summary> public static class FontHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 점 폰트 구하기 - GetDotFont(sourceTextBlock, dotSpace) /// <summary> /// 점 폰트 구하기 /// </summary> /// <param name="sourceTextBlock">소스 텍스트 블럭</param> /// <param name="dotSpace">점 공간</param> /// <returns>포인트 목록</returns> public static List<Point> GetDotFont(TextBlock sourceTextBlock, int dotSpace) { int textWidth = (int)sourceTextBlock.Width; int textHeight = (int)sourceTextBlock.Height; FormattedText formattedText = new FormattedText ( sourceTextBlock.Text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface ( sourceTextBlock.FontFamily, sourceTextBlock.FontStyle, sourceTextBlock.FontWeight, sourceTextBlock.FontStretch ), sourceTextBlock.FontSize, sourceTextBlock.Foreground ); DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); drawingContext.DrawText(formattedText, new Point(0, 0)); drawingContext.Close(); RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(textWidth, textHeight, 96, 96, PixelFormats.Pbgra32); renderTargetBitmap.Render(drawingVisual); BmpBitmapEncoder bmpBitmapEncoder = new BmpBitmapEncoder(); bmpBitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap)); MemoryStream memoryStream = new MemoryStream(); bmpBitmapEncoder.Save(memoryStream); memoryStream.Seek(54, SeekOrigin.Begin); byte[] bufferByteArray = new byte[memoryStream.Length - 54]; memoryStream.Read(bufferByteArray, 0, (int)(memoryStream.Length - 54)); List<Point> pointList = new List<Point>(); int heightOffSet = 0; for(int nY = 0; nY < textHeight; nY += dotSpace) { heightOffSet = textWidth * nY; for(int nX = 0; nX < textWidth; nX += dotSpace) { if(bufferByteArray[((heightOffSet + nX) << 2 )] == 255) { pointList.Add(new Point(nX, textHeight - nY)); } } } return pointList; } #endregion #region 점 폰트 그리기 - DrawDotFont(targetCanvas, pointList) /// <summary> /// 점 폰트 그리기 /// </summary> /// <param name="targetCanvas">타겟 캔버스</param> /// <param name="pointList">포인트 리스트</param> public static void DrawDotFont(Canvas targetCanvas, List<Point> pointList) { DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); foreach(Point point in pointList) { drawingContext.DrawRectangle ( Brushes.Black, null, new Rect(point.X, point.Y, 2, 2) ); } drawingContext.Close(); RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap ( (int)targetCanvas.ActualWidth, (int)targetCanvas.ActualHeight, 96, 96, PixelFormats.Pbgra32 ); renderTargetBitmap.Render(drawingVisual); targetCanvas.Background = new ImageBrush(renderTargetBitmap); } #endregion } } |
▶ MainWindow.xaml
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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="도트 폰트 생성하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="25" /> <RowDefinition Height="10" /> <RowDefinition Height="50*" /> <RowDefinition Height="10" /> <RowDefinition Height="50*" /> </Grid.RowDefinitions> <StackPanel Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Center" Orientation="Horizontal"> <TextBlock Width="70">텍스트</TextBlock> <TextBox x:Name="sourceTextBox" Margin="0 0 10 0" Width="100" Height="25" BorderBrush="Black" BorderThickness="1" Text="샘플 테스트" /> <Slider x:Name="spaceSlider" Width="150" Height="25" Margin="0,0,10,0" Minimum="1" Maximum="10" SmallChange="1" TickFrequency="1" Value="5" TickPlacement="BottomRight" /> <Button x:Name="generateButton" Width="80" Height="25" Content="생성" /> </StackPanel> <ListBox x:Name="targetListBox" Grid.Row="2" BorderBrush="Black" BorderThickness="1" /> <Border Grid.Row="4" BorderBrush="Black" BorderThickness="1"> <Canvas x:Name="targetCanvas" /> </Border> <TextBlock x:Name="targetTextBox" FontSize="96" FontFamily="맑은 고딕" Foreground="White" Background="Black" Width="{Binding ElementName=targetCanvas, Path=ActualWidth}" Height="{Binding ElementName=targetCanvas, Path=ActualHeight}" Visibility="Hidden" /> </Grid> </Window> |
▶ MainWindow.xaml.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 |
using System.Collections.Generic; using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.generateButton.Click += generateButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 생성 버튼 클릭시 처리하기 - generateButton_Click(sender, e) /// <summary> /// 생성 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void generateButton_Click(object sender, RoutedEventArgs e) { this.targetTextBox.Text = this.sourceTextBox.Text; List<Point> pointList = FontHelper.GetDotFont(this.targetTextBox, (int)this.spaceSlider.Value); FontHelper.DrawDotFont(this.targetCanvas, pointList); this.targetListBox.ItemsSource = pointList; } #endregion } } |