我需要导入多个图像,并在一个图像中操作它们,就像拼贴画一样。我只需要简单的工具,如翻译图像,旋转图像等等。
我在学习mvvm的过程中,大部分视图都已完成。我不知道如何将导入的图像添加到一个画布。我可以使用某种内置的图像类吗?或者我可以从哪个图书馆开始?
现在我可以将图像导入列表了。 我的目标是能够将所选图像添加到画布上,并在画布上操作该图像,然后导出/保存图像。
答案 0 :(得分:2)
然而你所看到的内容太宽泛而无法回答,但这是一个小样本
XAML
<Canvas x:Name="canvas" MouseDown="canvas_MouseDown" Background="Transparent"/>
背后的代码
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);
}
}
结果
上面的
也可以用MVVM方式完成,如果你也期待看到它,请告诉我。
MVVM方法
这是通过MVVM复制上述代码
XAML
<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>
我通过向模板添加更多详细信息来增强视图
Viewmodel,model&amp;转换器类
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();
}
}
}
结果