简体   繁体   English

如何在C#中使用WPF从多个导入的图像制作图像拼贴?

[英]How can I make an image collage from several imported images, using WPF in C#?

I need to import multiple images, and manipulated them in one image, like a collage. 我需要导入多张图像,并在一张图像中对其进行操作,例如拼贴。 I just need simple tools like translating an image, rotating it, ect. 我只需要简单的工具,例如平移图像,旋转图像等。

Im in the process of learning mvvm, most of the view is done. 在学习mvvm的过程中,大部分视图已完成。 I dont know how I can add the imported images to one canvas. 我不知道如何将导入的图像添加到一个画布。 Is there some sort of built in image class that I can use? 我可以使用某种内置的图像类吗? Or some library that I can start from? 还是我可以开始的一些图书馆?

Right now I am able to import the images to a list. 现在,我可以将图像导入列表了。 My goal is to be able to add the selected image onto the canvas, and manipulate that image on the canvas, then export/save the image. 我的目标是能够将选定的图像添加到画布上,并在画布上处理该图像,然后导出/保存该图像。

however what you are looking is too broad to answer but here is a small sample 但是,您要查找的内容过于宽泛,无法回答,但这是一个小样本

xaml a

    <Canvas x:Name="canvas" MouseDown="canvas_MouseDown" Background="Transparent"/>

code behind 背后的代码

    private void canvas_MouseDown(object sender, MouseButtonEventArgs e)
    {
        Random rand = new Random(DateTime.Now.Millisecond);
        var uriSource = new Uri(@"desert.jpg", UriKind.Relative);
        for (int i = 0; i < 10; i++)
        {
            Image img = new Image();
            img.Width = rand.NextDouble() * 200;
            img.Height = rand.NextDouble() * 200;
            img.Stretch= Stretch.UniformToFill;
            img.Source = new BitmapImage(uriSource);
            img.RenderTransform = new RotateTransform(rand.NextDouble() * 360);
            Canvas.SetLeft(img, (rand.NextDouble() * canvas.ActualWidth));
            Canvas.SetTop(img, (rand.NextDouble() * canvas.ActualHeight));
            canvas.Children.Add(img);
        }
    }

result 结果

结果

above could also be done in a MVVM way, let me know if you look forward to see that too. 以上也可以通过MVVM方式完成,如果您也希望看到这一点,请告诉我。

MVVM approach MVVM方法

here is a replication of the above code via MVVM 这是通过MVVM复制上面的代码

xaml a

<Grid>
    <Grid.Resources>
        <l:ViewModel x:Key="viewModel" />
        <l:ImageLocationConverter x:Key="ImageLocationConverter" />
        <Style TargetType="ContentPresenter">
            <Setter Property="Canvas.Left">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource ImageLocationConverter}">
                        <Binding Path="Left" />
                        <Binding Path="ActualWidth"
                                 RelativeSource="{RelativeSource FindAncestor,AncestorType=Canvas}" />
                    </MultiBinding>
                </Setter.Value>
            </Setter>
            <Setter Property="Canvas.Top">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource ImageLocationConverter}">
                        <Binding Path="Top" />
                        <Binding Path="ActualHeight"
                                 RelativeSource="{RelativeSource FindAncestor,AncestorType=Canvas}" />
                    </MultiBinding>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
    <ItemsControl ItemsSource="{Binding Images,Source={StaticResource viewModel}}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Border BorderBrush="White"
                            BorderThickness="5">
                        <Image Source="{Binding Image}"
                               Width="{Binding Width}"
                               Height="{Binding Height}"
                               Stretch="UniformToFill">
                        </Image>
                        <Border.RenderTransform>
                            <RotateTransform Angle="{Binding Angle}" />
                        </Border.RenderTransform>
                    </Border>
                    <Grid.Effect>
                        <DropShadowEffect Opacity=".5"
                                          BlurRadius="10" />
                    </Grid.Effect>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

I have enhanced the view by adding more details to the template 我通过向模板添加更多详细信息来增强了视图

Viewmodel, model & converters classes 视图模型,模型和转换器类

namespace CSharpWPF
{
    class ViewModel : DependencyObject
    {
        public ViewModel()
        {
            Images = new ObservableCollection<CollageImage>();
            GenerateCollage();
        }
        public ObservableCollection<CollageImage> Images { get; private set; }

        public void GenerateCollage()
        {
            Random rand = new Random(DateTime.Now.Millisecond);
            var uriSource = new Uri(@"desert.jpg", UriKind.Relative);
            for (int i = 0; i < 10; i++)
            {
                CollageImage img = new CollageImage();
                img.Left = (0.2 + rand.NextDouble()) % 0.8;
                img.Top = (0.2 + rand.NextDouble()) % 0.8;
                img.Width = 100 + rand.NextDouble() * 100;
                img.Height = 100 + rand.NextDouble() * 100;
                img.Image = "desert.jpg";
                img.Angle = rand.NextDouble() * 360;
                Images.Add(img);
            }
        }
    }

    class CollageImage
    {
        public string Image { get; set; }
        public double Left { get; set; }
        public double Top { get; set; }
        public double Width { get; set; }
        public double Height { get; set; }
        public double Angle { get; set; }
    }

    class ImageLocationConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double value = (double)values[0];
            double max = (double)values[1];

            return value * max;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

result 结果

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM