2014년 3월 16일 일요일

C# WPF로 도형 그려보기.

Canvas 클래스를 상속하여 선 그려보고 몇 변환을 가한 뒤 렌더링을 수행해 보았다.

OpenGL이나 DirectX와 유사한 메서드가 존재했다.

[XAML]
<Window x:Class="TryMyAngryEngine.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Name="rootGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Canvas Name="rootCanvas" Grid.Row="0">
            
        </Canvas>
        <Line
            Name="myLine"
            Grid.Row="1"
    X1="10" Y1="10"
    X2="50" Y2="50"
    StrokeThickness="4"
    Canvas.Left="100">
            <Line.Stroke>
                <RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5">
                    <RadialGradientBrush.GradientStops>
                        <GradientStop Color="Red" Offset="0" />
                        <GradientStop Color="Blue" Offset="0.25" />
                    </RadialGradientBrush.GradientStops>
                </RadialGradientBrush>
            </Line.Stroke>
        </Line>
    </Grid>
</Window>


[MAIN.CS]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TryMyAngryEngine
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            rootCanvas = new MyCanvas();
            rootCanvas.InvalidateVisual();
            //rootGrid.SetRow(rootCanvas, 0);

            Grid myGrid = new Grid();
            RowDefinition row3 = new RowDefinition();
            row3.Height = new GridLength(200, GridUnitType.Pixel);
            RowDefinition row = new RowDefinition();
            row.Height = new GridLength(1, GridUnitType.Star);
            RowDefinition row2 = new RowDefinition();
            row2.Height = new GridLength(1, GridUnitType.Star);
            ColumnDefinition col = new ColumnDefinition();
            col.Width = new GridLength(1, GridUnitType.Star);
            ColumnDefinition col2 = new ColumnDefinition();
            col2.Width = new GridLength(1, GridUnitType.Star);

            myGrid.RowDefinitions.Add(row3);
            myGrid.RowDefinitions.Add(row);
            myGrid.RowDefinitions.Add(row2);

            myGrid.ColumnDefinitions.Add(col);
            myGrid.ColumnDefinitions.Add(col2);
            

            Canvas c = new MyCanvas();

            Grid.SetRow(c, 0);
            Grid.SetColumn(c, 1);

            myGrid.ShowGridLines = true;

            myGrid.Children.Add(c);
            c = new MyCanvas();
            Grid.SetRow(c, 1);
            myGrid.Children.Add(c);
            //myGrid.Children.Add(this.myLine);

            this.Content = myGrid;


            Line myLine;
            myLine = new Line();
            myLine.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
            myLine.X1 = 1;
            myLine.X2 = 50;
            myLine.Y1 = 1;
            myLine.Y2 = 50;
            //myLine.HorizontalAlignment = HorizontalAlignment.Left;
            //myLine.VerticalAlignment = VerticalAlignment.Center;
            myLine.StrokeThickness = 2;
            rootCanvas.Children.Add(myLine);

            Ellipse myEllipse = new Ellipse();

            // Create a SolidColorBrush with a red color to fill the 
            // Ellipse with.
            SolidColorBrush mySolidColorBrush = new SolidColorBrush();

            // Describes the brush's color using RGB values. 
            // Each value has a range of 0-255.
            mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0);
            myEllipse.Fill = mySolidColorBrush;
            myEllipse.StrokeThickness = 2;
            myEllipse.Stroke = Brushes.Black;

            // Set the width and height of the Ellipse.
            myEllipse.Width = 200;
            myEllipse.Height = 100;
            rootCanvas.Children.Add(myEllipse);

            //rootCanvas.onRender();
            
        }
    }
}
[MyCanvas.cs]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using System.Windows.Media.Imaging;
using System.IO;

namespace TryMyAngryEngine
{
    class MyCanvas : Canvas
    {
        protected override void OnRender(System.Windows.Media.DrawingContext dc)
        {
            base.OnRender(dc);
            dc.DrawLine(new System.Windows.Media.Pen(Brushes.LightSteelBlue, 2), new Point(1, 1), new Point(50, 50));

            TranslateTransform tr = new TranslateTransform(10.0, -9.0);
            dc.PushTransform(tr);
            dc.DrawLine(new System.Windows.Media.Pen(Brushes.LightSteelBlue, 2), new Point(1, 1), new Point(50, 50));
            
            dc.Pop();

            dc.DrawLine(new System.Windows.Media.Pen(Brushes.LimeGreen, 2), new Point(1, 1), new Point(50, 50));

            PathFigure myPathFigure = new PathFigure();
            myPathFigure.StartPoint = new Point(10, 50);
            myPathFigure.Segments.Add(
                new BezierSegment(
                    new Point(100, 0),
                    new Point(200, 200),
                    new Point(300, 100),
                    true /* IsStroked */  ));
            myPathFigure.Segments.Add(
                new LineSegment(
                    new Point(400, 100),
                    true /* IsStroked */ ));
            myPathFigure.Segments.Add(
                new ArcSegment(
                    new Point(200, 100),
                    new Size(50, 50),
                    45,
                    true, /* IsLargeArc */
                    SweepDirection.Clockwise,
                    true /* IsStroked */ ));

            /// Create a PathGeometry to contain the figure.
            PathGeometry myPathGeometry = new PathGeometry();
            myPathGeometry.Figures.Add(myPathFigure);

            RotateTransform rt = new RotateTransform(30.0, 0.0, 0.0);
            dc.DrawGeometry(Brushes.Magenta, new Pen(Brushes.Red, 1), myPathGeometry);
            dc.PushTransform(rt);

            dc.DrawGeometry(Brushes.MediumBlue, new Pen(Brushes.Red, 1), myPathGeometry);
            dc.Pop();

            ScaleTransform st = new ScaleTransform(1.0, 0.5);
            dc.PushTransform(st);
            dc.DrawGeometry(Brushes.MediumBlue, new Pen(Brushes.Red, 1), myPathGeometry);
            dc.Pop();

            Uri myUri = new Uri("http://img.izismile.com/img/img6/20130304/640/naturally_beautiful_girls_640_20.jpg", UriKind.RelativeOrAbsolute);
            BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            
            
            JpegBitmapDecoder decoder2 = new JpegBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
            
            BitmapSource bitmapSource2 = decoder2.Frames[0];
            

            dc.PushTransform(rt);
            dc.DrawImage(bitmapSource2, new Rect(0.0, 0.0, 640, 426));
            dc.Pop();
            

            string uri = "http://images5.fanpop.com/image/photos/31800000/Whitebeards-after-the-escape-from-Shiki-edward-whitebeard-newgate-31858048-500-282.jpg";
            myUri = new Uri(uri, UriKind.RelativeOrAbsolute);
            decoder2 = new JpegBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
            bitmapSource2 = decoder2.Frames[0];

            dc.DrawImage(bitmapSource2, new Rect(182.0, 12.0, 50, 28));
        }
    }
}

[결과]
출처(이미지) : http://img.izismile.com/img/img6/20130304/640/naturally_beautiful_girls_640_20.jpg,
: http://images5.fanpop.com/image/photos/31800000/Whitebeards-after-the-escape-from-Shiki-edward-whitebeard-newgate-31858048-500-282.jpg

참고 사이트
http://msdn.microsoft.com/ko-kr/library/ms748373(v=vs.110).aspx
http://msdn.microsoft.com/ko-kr/library/system.windows.controls.panel(v=vs.110).aspx
http://msdn.microsoft.com/ko-kr/library/system.windows.controls.panel.onrender(v=vs.110).aspx
http://msdn.microsoft.com/ko-kr/library/system.windows.controls.canvas(v=vs.110).aspx
http://msdn.microsoft.com/ko-kr/library/ms747393(v=vs.110).aspx
http://msdn.microsoft.com/ko-kr/library/system.windows.media.transform(v=vs.110).aspx
http://msdn.microsoft.com/ko-kr/library/ms750596(v=vs.110).aspx
http://msdn.microsoft.com/ko-kr/library/ms751808(v=vs.110).aspx
http://msdn.microsoft.com/ko-kr/library/ms748873(v=vs.110).aspx

댓글 없음:

댓글 쓰기