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

728x90
반응형

■ RenderTargetBitmap 클래스 : Render 메소드를 사용해 이미지 만들기

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


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="RenderTargetBitmap 클래스 : Render 메소드를 사용해 이미지 만들기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Grid>

        <Image Name="image"

            HorizontalAlignment="Center"

            VerticalAlignment="Center"

            Stretch="None" />

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Globalization;

using System.Windows;

using System.Windows.Media;

using System.Windows.Media.Imaging;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            Loaded += Window_Loaded;

        }

 

        #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)

        {

            FormattedText formattedText = new FormattedText

            (

                "ABC",

                new CultureInfo("en-us"),

                FlowDirection.LeftToRight,

                new Typeface

                (

                    new FontFamily("Times New Roman"),

                    FontStyles.Normal,

                    FontWeights.Normal,

                    new FontStretch()

                ),

                96,

                Brushes.White

            );

 

            DrawingVisual drawingVisual = new DrawingVisual();

 

            DrawingContext drawingContext = drawingVisual.RenderOpen();

 

            drawingContext.DrawRectangle

            (

                Brushes.RoyalBlue,

                new Pen(Brushes.RoyalBlue, 0),

                new Rect

                (

                    0,

                    0,

                    formattedText.Width,

                    formattedText.Height

                )

            );

 

            drawingContext.DrawText(formattedText, new Point(0, 0));

 

            drawingContext.Close();

 

            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap

            (

                (int)formattedText.Width,

                (int)formattedText.Height,

                96,

                96,

                PixelFormats.Pbgra32

            );

 

            renderTargetBitmap.Render(drawingVisual);

 

            this.image.Source = renderTargetBitmap;

        }

 

        #endregion

    }

}

 

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

728x90
반응형
Posted by 사용자 icodebroker

댓글을 달아 주세요