728x90
반응형
728x170
▶ FontHelper.cs
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
}
}
728x90
▶ MainWindow.xaml
<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>
300x250
▶ MainWindow.xaml.cs
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
}
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] Shape 클래스 : 펜 사용하기 (0) | 2018.02.18 |
---|---|
[C#/WPF] 파티클 애니메이션 사용하기 (0) | 2018.02.18 |
[C#/WPF] 마우스 애니메이션 사용하기 (0) | 2018.02.18 |
[C#/WPF] 하이브리드 시계 사용하기 (0) | 2018.02.18 |
[C#/WPF] 마우스 드래그를 사용해 원 그리기 (0) | 2018.02.18 |
[C#/WPF] 글자 애니메이션 사용하기 (0) | 2018.02.18 |
[C#/WPF] MarkupExtension 클래스 : 마크업 확장 사용하기 (0) | 2018.02.18 |
[C#/WPF] MediaElement 클래스 : 동영상 재생하기 (0) | 2017.06.14 |
[C#/WPF] ListView 클래스 : 그리드 뷰 컬럼 헤더 클릭시 정렬하기 (0) | 2017.06.11 |
[C#/WPF] DataGrid 클래스 : RowValidationRules 속성을 사용해 검증하기 (0) | 2017.06.11 |
댓글을 달아 주세요