728x90
반응형

/// <summary>

/// 이미지 사각형을 그림니다

/// </summary>

/// <param name="imageSource">이미지입니다.</param>

/// <param name="rect">사각형입니다.</param>

/// <param name="angle">사각형의 Angle 값입니다</param>

/// <returns>이미지에 Angle 이 적용된 사각형이 그려져 반환됩니다</returns>

public static ImageSource ConvertVisionImageInfoToScanImageSource(ImageSource imageSource, Rect rect, double angle)

{

    System.Windows.Media.Imaging.BitmapSource bImage = imageSource as System.Windows.Media.Imaging.BitmapSource;

 

    DrawingVisual dVisual = new DrawingVisual();

    using (DrawingContext dc = dVisual.RenderOpen())

    {

        dc.DrawImage(bImage, new Rect(0, 0, bImage.PixelWidth, bImage.PixelHeight));

 

        // 원본이미지 중심 기준으로 Angle 을 돌립니다.

        dc.PushTransform(new RotateTransform(angle, bImage.PixelWidth / 2, bImage.PixelHeight / 2));

        dc.DrawRectangle((SolidColorBrush)(new BrushConverter().ConvertFrom(Colors.Blue)), null, rect);

    }

 

    System.Windows.Media.Imaging.RenderTargetBitmap targetBitmap = new System.Windows.Media.Imaging.RenderTargetBitmap(bImage.PixelWidth, bImage.PixelHeight, bImage.DpiX, bImage.DpiY, PixelFormats.Default);

    targetBitmap.Render(dVisual);

 

    System.Windows.Media.Imaging.WriteableBitmap wBitmap = new System.Windows.Media.Imaging.WriteableBitmap(targetBitmap);

 

    return wBitmap;

}

 

728x90
반응형
728x90
반응형

 

 

 

using System.Collections.Generic;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Windows;

using System.Windows.Media;

 

 

/// <summary>

/// Convert GraphicsPath To PathGeometry

/// </summary>

/// <param name="path">System.Drawing.Drawing2D.GraphicsPath</param>

/// <returns>System.Windows.Media.PathGeometry</returns>

public static PathGeometry ConvertGraphicsPathToPathGeometry(GraphicsPath path)

{

    List<PathFigure> pathFigureList = new List<PathFigure>();

 

    if (path.PointCount != 0)

    {

        PointF[] pathPoints = path.PathPoints;

        byte[] pathTypes = path.PathTypes;

        PathFigure pathFigure = new PathFigure();

        pathFigureList.Add(pathFigure);

        int index = 0;

        while (index < pathPoints.Length)

        {

            switch ((int)pathTypes[index] & 7)

            {

                case 0:

                    System.Windows.Point windowsPoint1 = new System.Windows.Point((double)pathPoints[index].X, (double)pathPoints[index].Y); ;

                    pathFigure.StartPoint = windowsPoint1;

                    ++index;

                    break;

                case 1:

                    System.Windows.Point windowsPoint2 = new System.Windows.Point((double)pathPoints[index].X, (double)pathPoints[index].Y);

                    pathFigure.Segments.Add((PathSegment)new LineSegment(windowsPoint2, true));

                    ++index;

                    break;

                case 3:

                    System.Windows.Point windowsPoint3 = new System.Windows.Point((double)pathPoints[index].X, (double)pathPoints[index].Y);

                    System.Windows.Point windowsPoint4 = new System.Windows.Point((double)pathPoints[index + 1].X, (double)pathPoints[index + 1].Y);

                    System.Windows.Point windowsPoint5 = new System.Windows.Point((double)pathPoints[index + 2].X, (double)pathPoints[index + 2].Y);

                    pathFigure.Segments.Add((PathSegment)new BezierSegment(windowsPoint3, windowsPoint4, windowsPoint5, true));

                    index += 3;

                    break;

            }

 

            if (((int)pathTypes[index - 1] & 128) != 0)

            {

                pathFigure.IsClosed = true;

                pathFigure = new PathFigure();

                pathFigureList.Add(pathFigure);

            }

        }

    }

 

    return new PathGeometry((IEnumerable<PathFigure>)pathFigureList) { FillRule = path.FillMode == FillMode.Alternate ? FillRule.EvenOdd : FillRule.Nonzero };

}

 

728x90
반응형
728x90
반응형

MainWindow.xaml

<Window x:Class="ImageZoomPanning.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:ImageZoomPanning"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Grid x:Name="LayoutRoot" Height="300">

        <Grid.RowDefinitions>

            <RowDefinition Height="52.92"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

 

        <Border Grid.Row="1" Name="border" Background="Gray" ClipToBounds="True">

            <Image Name="image" Source="iron-man-wallpaper-hd-9.jpg" Opacity="1" RenderTransformOrigin="0.5,0.5" ClipToBounds="True" />

        </Border>

 

    </Grid>

</Window>

MainWindow.xaml.cs

using System.Linq;

using System.Windows;

using System.Windows.Input;

using System.Windows.Media;

 

namespace ImageZoomPanning

{

    /// <summary>

    /// MainWindow.xaml에 대한 상호 작용 논리

    /// </summary>

    public partial class MainWindow : Window

    {

        /// <summary>

        /// 원본 포인트입니다.

        /// </summary>

        private Point origin;

        /// <summary>

        /// 시작 포인트입니다.

        /// </summary>

        private Point start;

 

        /// <summary>

        /// 생성자입니다.

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            TransformGroup transformGroup = new TransformGroup();

 

            ScaleTransform scaleTransform = new ScaleTransform();

            transformGroup.Children.Add(scaleTransform);

 

            TranslateTransform translateTransform = new TranslateTransform();

            transformGroup.Children.Add(translateTransform);

 

            image.RenderTransform = transformGroup;

 

            image.MouseWheel += image_MouseWheel;

 

            image.MouseLeftButtonDown += image_MouseLeftButtonDown;

            image.MouseLeftButtonUp += image_MouseLeftButtonUp;

            image.MouseMove += image_MouseMove;

        }

 

        /// <summary>

        /// 마우스 휠 움직임에 따라 Zoon In/Out 합니다.

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void image_MouseWheel(object sender, MouseWheelEventArgs e)

        {

            var transform = (ScaleTransform)((TransformGroup)image.RenderTransform).Children.First(c => c is ScaleTransform);

 

            double zoom = e.Delta > 0 ? .2 : -.2;

            transform.ScaleX += zoom;

            transform.ScaleY += zoom;

        }

 

        /// <summary>

        /// 좌클릭에 따라 패닝을 시작합니다.

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

        {

            image.CaptureMouse();

            var translateTransform = (TranslateTransform)((TransformGroup)image.RenderTransform).Children.First(c => c is TranslateTransform);

            start = e.GetPosition(border);

            origin = new Point(translateTransform.X, translateTransform.Y);

        }

 

        /// <summary>

        /// 마우스 우버튼 놓을때 패닝되던 이미지가 멈춥니다

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

        {

            image.ReleaseMouseCapture();

        }

 

        /// <summary>

        /// 마우스 움직임에 따라 이미지가 패닝됩니다.

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void image_MouseMove(object sender, MouseEventArgs e)

        {

            if (!image.IsMouseCaptured) return;

 

            var translateTransform = (TranslateTransform)((TransformGroup)image.RenderTransform).Children.First(c => c is TranslateTransform);

            Vector v = start - e.GetPosition(border);

            translateTransform.X = origin.X - v.X;

            translateTransform.Y = origin.Y - v.Y;

        }

    }

}

 

 

https://youtu.be/VOmAMaQc2Xg

728x90
반응형
728x90
반응형

(SolidColorBrush)(new BrushConverter().ConvertFrom("#ffaacc"))

728x90
반응형
728x90
반응형

개행시 &#10; 를 사용

<TextBlock Text="Test&#10;Group"/>

[결과]
Test
Group

 

 

728x90
반응형
728x90
반응형

XAML 에서 소수점 숫자를 3자리 까지만 표현하고자 할 때 아래처럼 사용하면 된다.

 

 

 

<TextBlock Text="{Binding Number, StringFormat={}Y:{0:0.###}}" />

 

 

Number : 56.235487

결과

Y:56.235

728x90
반응형
728x90
반응형

Property 중 특정 두 값이 조건에 해당 되는 경우 처리를 위해서는 MiltiDataTrigger 를 사용합니다.

 

아래는 StringData1 속성의 값이 "Data1" 이고 StringData2 값이 "Data2" 인경우

Grid 를 숨기는(Visibility=Collapsed) 처리를 합니다.

 

 

    <Grid>

        <Grid.Style>

            <Style TargetType="Grid">

                <Style.Triggers>

                    <MultiDataTrigger>

                        <MultiDataTrigger.Conditions>

                            <Condition Binding="{Binding StringData1}" Value="Data1"/>

                            <Condition Binding="{Binding StringData2}" Value="Data2"/>

                        </MultiDataTrigger.Conditions>

                        <Setter Property="Visibility" Value="Collapsed"/>

                    </MultiDataTrigger>

                </Style.Triggers>

            </Style>

        </Grid.Style>

    </Grid>

 

 

728x90
반응형
728x90
반응형

MultiDataTrigger 는 And 조건을 처리하고

OR 조건은 DataTrigger 를 두개 사용하여 처리합니다.

 

아래는 StringData(string) Property 에 null, 공백여부 두가지를 or 조건으로 처리한 내용입니다.

 

 

        <DataTrigger Binding="{Binding StringData}" Value="{x:Null}">

            <Setter Property="Visibility" Value="Hidden" />

        </DataTrigger>

        <DataTrigger Binding="{Binding StringData}" Value="{x:Static System:String.Empty}" >

            <Setter Property="Visibility" Value="Hidden" />

        </DataTrigger>

 

 

 

728x90
반응형
728x90
반응형

ListView 내부의 Button 클릭시

해당 버튼이 속해있는 Item 을 가져오는 방법입니다.

 

 

 

private void DeleteButton_Click(object sender, RoutedEventArgs e)

{

    var listViewItem = (ListViewItem)listView.ItemContainerGenerator.ContainerFromItem(((Button)sender).DataContext);

   TestItemModel testItemModel = listViewItem.Content as TestItemModel;

}

 

 

728x90
반응형
728x90
반응형
<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListViewItem">
                    <ContentPresenter/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ListView.ItemContainerStyle>

선택한 항목에 대해서 표시를 하고 싶지 않을 때 사용합니다.

* 선택에 대한 이벤트도 타지 않으니 단순 표시를 위해서만 사용합니다.

728x90
반응형

+ Recent posts