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

728x90
반응형

■ FrameworkElement 클래스 : 이미지 저장하기

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


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="FrameworkElement 클래스 : 이미지 저장하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Grid>

        <StackPanel

            HorizontalAlignment="Center"

            VerticalAlignment="Center">

            <StackPanel

                Margin="10"

                Orientation="Horizontal">

                <StackPanel.Resources>

                    <Style TargetType="Canvas">

                        <Setter Property="Margin"     Value="10"          />

                        <Setter Property="Width"      Value="20"          />

                        <Setter Property="Height"     Value="20"          />

                        <Setter Property="Background" Value="Transparent" />

                    </Style>

                    <Style TargetType="Line">

                        <Setter Property="StrokeThickness"    Value="4"     />

                        <Setter Property="Stroke"             Value="Blue" />

                        <Setter Property="StrokeStartLineCap" Value="Round" />

                        <Setter Property="StrokeEndLineCap"   Value="Round" />

                    </Style>

                    <Style TargetType="Polyline">

                        <Setter Property="StrokeThickness"    Value="4"     />

                        <Setter Property="Stroke"             Value="Blue" />

                        <Setter Property="StrokeEndLineCap"   Value="Round" />

                        <Setter Property="StrokeStartLineCap" Value="Round" />

                    </Style>

                </StackPanel.Resources>

                <Canvas Name="xCanvas">

                    <Line

                        X1="2"

                        Y1="2"

                        X2="16"

                        Y2="16"

                        StrokeThickness="3.5" />

                    <Line

                        X1="2"

                        Y1="16"

                        X2="16"

                        Y2="2"

                        StrokeThickness="3.5" />

                </Canvas>

                <Canvas Name="plusCanvas">

                    <Line

                        X1="2"

                        Y1="10"

                        X2="18"

                        Y2="10" />

                    <Line

                        X1="10"

                        Y1="18"

                        X2="10"

                        Y2="2" />

                </Canvas>

                <Canvas Name="arrowUpCanvas">

                    <Polyline Points="2 14 10 6 18 14" />

                </Canvas>

                <Canvas Name="arrowDownCanvas">

                    <Polyline Points="2 6 10 14 18 6" />

                </Canvas>

                <Canvas Name="pencilCanvas">

                    <Line

                        X1="2"

                        Y1="18"

                        X2="13"

                        Y2="7"

                        StrokeThickness="6"

                        StrokeStartLineCap="Triangle"

                        StrokeEndLineCap="Flat" />

                    <Line

                        X1="14"

                        Y1="6"

                        X2="17"

                        Y2="3"

                        StrokeThickness="6"

                        StrokeStartLineCap="Flat"

                        StrokeEndLineCap="Flat" />

                </Canvas>

            </StackPanel>

            <Button Name="captureButton"

                Width="100"

                Height="30"

                Content="Capture" />

        </StackPanel>

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System;

using System.IO;

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

 

            this.captureButton.Click += captureButton_Click;

        }

 

        #endregion

 

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

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

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region Capture 버튼 클릭시 처리하기 - captureButton_Click(sender, e)

 

        /// <summary>

        /// Capture 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void captureButton_Click(object sender, RoutedEventArgs e)

        {

            try

            {

                SaveImage(this.xCanvas        , "d:\\x.png"         );

                SaveImage(this.plusCanvas     , "d:\\plus.png"      );

                SaveImage(this.arrowUpCanvas  , "d:\\arrow_up.png"  );

                SaveImage(this.arrowDownCanvas, "d:\\arrow_down.png");

                SaveImage(this.pencilCanvas   , "d:\\pencil.png"    );

 

                MessageBox.Show("작업을 완료했습니다.");

            }

            catch(Exception exception)

            {

                MessageBox.Show(exception.Message);

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 이미지 저장하기 - SaveImage(element, filePath)

 

        /// <summary>

        /// 이미지 저장하기

        /// </summary>

        /// <param name="element">프레임워크 엘리먼트</param>

        /// <param name="filePath">파일 경로</param>

        private void SaveImage(FrameworkElement element, string filePath)

        {

            Rect rectangle = new Rect(0, 0, element.ActualWidth, element.ActualHeight);

 

            DrawingVisual visual = new DrawingVisual();

 

            using(DrawingContext context = visual.RenderOpen())

            {

                VisualBrush brush = new VisualBrush(element);

 

                context.DrawRectangle(brush, null, new Rect(rectangle.Size));

            }

 

            int width  = (int)element.ActualWidth;

            int height = (int)element.ActualHeight;

 

            RenderTargetBitmap bitmap = new RenderTargetBitmap

            (

                width,

                height,

                96,

                96,

                PixelFormats.Pbgra32

            );

 

            bitmap.Render(visual);

 

            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

    }

}

 

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

728x90
반응형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요