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

■ 이미지 텍스트 그리기

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


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

    Height="450"

    Title="이미지 텍스트 그리기"

    Background="Transparent">

    <Grid Name="mainGrid">

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System;

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();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 렌더링시 처리하기 - OnRender(drawingContext)

 

        /// <summary>

        /// 렌더링시 처리하기

        /// </summary>

        /// <param name="drawingContext">그리기 컨텍스트</param>

        protected override void OnRender(DrawingContext drawingContext)

        {

            Rect backgroundRectangle = new Rect(0, 0, ActualWidth, ActualHeight);

 

            drawingContext.DrawRectangle(Brushes.White, null, backgroundRectangle);

 

            const double FONT_SIZE = 90;

 

            FontFamily fontFamily = new FontFamily("Verdana");

            Typeface   typeface   = new Typeface("Verdana");

 

            FormattedText formattedText = new FormattedText

            (

                "Flowers",

                CultureInfo.CurrentCulture,

                FlowDirection.LeftToRight,

                typeface,

                FONT_SIZE,

                Brushes.Black

            );

 

            formattedText.SetFontWeight(FontWeights.Bold);

 

            formattedText.TextAlignment = TextAlignment.Center;

 

            Point originPoint = new Point

            (

                this.mainGrid.ActualWidth / 2,

                (this.mainGrid.ActualHeight - formattedText.Height) / 2

            );

 

            Geometry geometry = formattedText.BuildGeometry(originPoint);

 

            ImageSource imageSource = new BitmapImage(new Uri("Flowers.jpg", UriKind.Relative));

 

            ImageBrush imageBrush = new ImageBrush(imageSource);

 

            imageBrush.Stretch = Stretch.UniformToFill;

 

            Pen pen = new Pen(Brushes.Green, 2);

 

            drawingContext.DrawGeometry(imageBrush, pen, geometry);

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요