■ DropShadowBitmapEffect 클래스를 사용해 그림자를 갖는 텍스트 이미지를 구하는 방법을 보여준다.
▶ 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 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 |
<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="DropShadowBitmapEffect 클래스 : 그림자를 갖는 텍스트 이미지 구하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid.Resources> <Style TargetType="Label"> <Setter Property="Margin" Value="2" /> </Style> <Style TargetType="TextBox"> <Setter Property="Margin" Value="2" /> </Style> <Style TargetType="Button"> <Setter Property="Margin" Value="5" /> </Style> </Grid.Resources> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Label Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" Content="텍스트" /> <TextBox Name="textTextBox" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" HorizontalAlignment="Left" VerticalAlignment="Center" Text="Favorite Books" /> <Label Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" Content="폰트" /> <TextBox Name="fontTextBox" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" HorizontalAlignment="Left" VerticalAlignment="Center" Text="Brush Script MT" /> <Label Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" Content="폰트 크기" /> <TextBox Name="fontSizeTextBox" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" MinWidth="50" Text="72" /> <Label Grid.Row="3" Grid.Column="0" VerticalAlignment="Center" Content="폰트 가중치" /> <ComboBox Name="fontWeightComboBox" Grid.Row="3" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" MinWidth="100" /> <Label Grid.Row="4" Grid.Column="0" VerticalAlignment="Center" Content="텍스트 색상" /> <StackPanel Grid.Row="4" Grid.Column="1" Orientation="Horizontal"> <StackPanel.Resources> <Style TargetType="Canvas"> <Setter Property="Margin" Value="0 0 5 0"/> </Style> </StackPanel.Resources> <Canvas Width="20" Height="20" Background="Black" MouseDown="textColorCanvas_MouseDown" /> <Canvas Width="20" Height="20" Background="White" MouseDown="textColorCanvas_MouseDown" /> <Canvas Width="20" Height="20" Background="Green" MouseDown="textColorCanvas_MouseDown" /> <Canvas Width="20" Height="20" Background="Blue" MouseDown="textColorCanvas_MouseDown" /> <Canvas Width="20" Height="20" Background="Red" MouseDown="textColorCanvas_MouseDown" /> <Canvas Width="20" Height="20" Background="Yellow" MouseDown="textColorCanvas_MouseDown" /> <Canvas Width="20" Height="20" Background="Orange" MouseDown="textColorCanvas_MouseDown" /> </StackPanel> <Label Grid.Row="5" Grid.Column="0" VerticalAlignment="Center" Content="그림자 색상" /> <StackPanel Grid.Row="5" Grid.Column="1" Orientation="Horizontal"> <StackPanel.Resources> <Style TargetType="Canvas"> <Setter Property="Margin" Value="0 0 5 0" /> </Style> </StackPanel.Resources> <Canvas Width="20" Height="20" Background="Black" MouseDown="shadowColorCanvas_MouseDown" /> <Canvas Width="20" Height="20" Background="White" MouseDown="shadowColorCanvas_MouseDown" /> <Canvas Width="20" Height="20" Background="Green" MouseDown="shadowColorCanvas_MouseDown" /> <Canvas Width="20" Height="20" Background="Blue" MouseDown="shadowColorCanvas_MouseDown" /> <Canvas Width="20" Height="20" Background="Red" MouseDown="shadowColorCanvas_MouseDown" /> <Canvas Width="20" Height="20" Background="Yellow" MouseDown="shadowColorCanvas_MouseDown" /> <Canvas Width="20" Height="20" Background="Orange" MouseDown="shadowColorCanvas_MouseDown" /> </StackPanel> <Grid Name="textGrid" Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="3" Background="Transparent"> <Label Name="resultLabel" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Brush Script MT" FontSize="50" Content="Favorite Books"> <Label.BitmapEffect> <DropShadowBitmapEffect Color="Black" Direction="-45" ShadowDepth="10" Softness=".7" /> </Label.BitmapEffect> </Label> </Grid> <Button Name="saveButton" Grid.Row="7" Grid.Column="0" Grid.ColumnSpan="2" Width="120" Height="30" Content="이미지 저장" /> </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 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
using Microsoft.Win32; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.IO; using System.Windows.Media; using System.Windows.Media.Effects; using System.Windows.Media.Imaging; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 텍스트 브러시 /// </summary> private SolidColorBrush textBrush = Brushes.Black; /// <summary> /// 그림자 브러시 /// </summary> private SolidColorBrush shadowBrush = Brushes.Black; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); Loaded += Window_Loaded; this.textTextBox.TextChanged += textTextBox_TextChanged; this.fontTextBox.TextChanged += fontTextBox_TextChanged; this.fontSizeTextBox.TextChanged += fontSizeTextBox_TextChanged; this.fontWeightComboBox.SelectionChanged += fontWeightComboBox_SelectionChanged; this.saveButton.Click += saveButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { this.fontWeightComboBox.Items.Add(FontWeights.Thin ); this.fontWeightComboBox.Items.Add(FontWeights.ExtraLight); this.fontWeightComboBox.Items.Add(FontWeights.Light ); this.fontWeightComboBox.Items.Add(FontWeights.Regular ); this.fontWeightComboBox.Items.Add(FontWeights.Medium ); this.fontWeightComboBox.Items.Add(FontWeights.SemiBold ); this.fontWeightComboBox.Items.Add(FontWeights.Bold ); this.fontWeightComboBox.Items.Add(FontWeights.ExtraBold ); this.fontWeightComboBox.Items.Add(FontWeights.Black ); this.fontWeightComboBox.Items.Add(FontWeights.ExtraBlack); this.fontWeightComboBox.SelectedIndex = 3; } #endregion #region 텍스트 텍스트 박스 텍스트 변경시 처리하기 - textTextBox_TextChanged(sender, e) /// <summary> /// 텍스트 텍스트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void textTextBox_TextChanged(object sender, TextChangedEventArgs e) { ShowText(); } #endregion #region 폰트 텍스트 박스 텍스트 변경시 처리하기 - fontTextBox_TextChanged(sender, e) /// <summary> /// 폰트 텍스트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void fontTextBox_TextChanged(object sender, TextChangedEventArgs e) { ShowText(); } #endregion #region 폰트 크기 텍스트 박스 텍스트 변경시 처리하기 - fontSizeTextBox_TextChanged(sender, e) /// <summary> /// 폰트 크기 텍스트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void fontSizeTextBox_TextChanged(object sender, TextChangedEventArgs e) { ShowText(); } #endregion #region 폰트 가중치 콤보 박스 선택 변경시 처리하기 - fontWeightComboBox_SelectionChanged(sender, e) /// <summary> /// 폰트 가중치 콤보 박스 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void fontWeightComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ShowText(); } #endregion #region 텍스트 색상 캔버스 마우스 DOWN 처리하기 - textColorCanvas_MouseDown(sender, e) /// <summary> /// 텍스트 색상 캔버스 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void textColorCanvas_MouseDown(object sender, MouseButtonEventArgs e) { Canvas canvas = sender as Canvas; this.textBrush = (SolidColorBrush)canvas.Background; ShowText(); } #endregion #region 그림자 색상 캔버스 마우스 DOWN 처리하기 - shadowColorCanvas_MouseDown(sender, e) /// <summary> /// 그림자 색상 캔버스 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void shadowColorCanvas_MouseDown(object sender, MouseButtonEventArgs e) { Canvas canvas = sender as Canvas; this.shadowBrush = (SolidColorBrush)canvas.Background; ShowText(); } #endregion #region 이미지 저장 버튼 클릭시 처리하기 - saveButton_Click(sender, e) /// <summary> /// 이미지 저장 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void saveButton_Click(object sender, RoutedEventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.FileName = this.textTextBox.Text; saveFileDialog.Filter = "PNG Files|*.png|All files|*.*"; saveFileDialog.FilterIndex = 0; saveFileDialog.DefaultExt = ".png"; if(saveFileDialog.ShowDialog() == true) { SaveImage(this.textGrid, saveFileDialog.FileName); } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 텍스트 표시하기 - ShowText() /// <summary> /// 텍스트 표시하기 /// </summary> private void ShowText() { if(!this.IsLoaded) { return; } try { this.resultLabel.FontFamily = new FontFamily(this.fontTextBox.Text); this.resultLabel.FontSize = int.Parse(this.fontSizeTextBox.Text); this.resultLabel.FontWeight = (FontWeight)this.fontWeightComboBox.SelectedItem; this.resultLabel.Foreground = this.textBrush; this.resultLabel.Content = this.textTextBox.Text; DropShadowBitmapEffect effect = this.resultLabel.BitmapEffect as DropShadowBitmapEffect; effect.Color = this.shadowBrush.Color; } catch { } } #endregion #region 이미지 저장하기 - SaveImage(frameworkElement, filePath) /// <summary> /// 이미지 저장하기 /// </summary> /// <param name="frameworkElement">프레임워크 엘리먼트</param> /// <param name="filePath">파일 경로</param> private void SaveImage(FrameworkElement frameworkElement, string filePath) { Rect rectangle = VisualTreeHelper.GetDescendantBounds(frameworkElement); DrawingVisual drawingVisual = new DrawingVisual(); using(DrawingContext drawingContext = drawingVisual.RenderOpen()) { VisualBrush visualBrush = new VisualBrush(frameworkElement); drawingContext.DrawRectangle(visualBrush, null, new Rect(rectangle.Size)); } int width = (int)frameworkElement.ActualWidth; int height = (int)frameworkElement.ActualHeight; RenderTargetBitmap bitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); bitmap.Render(drawingVisual); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmap)); using(FileStream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { encoder.Save(stream); } } #endregion } } |