첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ DropShadowBitmapEffect 클래스 : 그림자를 갖는 텍스트 이미지 구하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip



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="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

 

 

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

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요