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

728x90
반응형

■ DrawingContext 클래스 : DrawImage 메소드를 사용해 이미지 그리기

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


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="DrawingContext 클래스 : DrawImage 메소드를 사용해 이미지 그리기"

    Background="Transparent">

    <Grid Name="grid" >

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System;

using System.Windows;

using System.Windows.Media;

using System.Windows.Media.Imaging;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 비트맵 이미지

        /// </summary>

        private BitmapImage bitmapImage;

 

        #endregion

 

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            this.bitmapImage = new BitmapImage();

 

            this.bitmapImage.BeginInit();

 

            this.bitmapImage.DecodePixelWidth = 300;

            this.bitmapImage.UriSource        = GetResourceURI(null, "IMAGE/sample.jpg");

 

            this.bitmapImage.EndInit();

        }

 

        #endregion

 

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

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

 

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

 

        /// <summary>

        /// 렌더링시 처리하기

        /// </summary>

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

        protected override void OnRender(DrawingContext drawingContext)

        {

            if(this.bitmapImage != null)

            {

                drawingContext.DrawRectangle

                (

                    Brushes.White,

                    null,

                    new Rect

                    (

                        0,

                        0,

                        this.grid.ActualWidth,

                        this.grid.ActualHeight

                    )

                );

 

                Rect rectangle = new Rect

                (

                    (this.grid.ActualWidth  - this.bitmapImage.PixelWidth ) / 2,

                    (this.grid.ActualHeight - this.bitmapImage.PixelHeight) / 2,

                    this.bitmapImage.PixelWidth,

                    this.bitmapImage.PixelHeight

                );

 

                drawingContext.DrawImage(this.bitmapImage, rectangle);

            }

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 리소스 URI 구하기 - GetResourceURI(assemblyName, resourcePath)

 

        /// <summary>

        /// 리소스 URI 구하기

        /// </summary>

        /// <param name="assemblyName">어셈블리명</param>

        /// <param name="resourcePath">리소스 경로</param>

        /// <returns>리소스 URI</returns>

        private Uri GetResourceURI(string assemblyName, string resourcePath)

        {

            if(string.IsNullOrEmpty(assemblyName))

            {

                return new Uri(string.Format("pack://application:,,,/{0}", resourcePath));

            }

            else

            {

                return new Uri(string.Format("pack://application:,,,/{0};component/{1}", assemblyName, resourcePath));

            }

        }

 

        #endregion

    }

}

 

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

728x90
반응형
Posted by 사용자 icodebroker

댓글을 달아 주세요