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. 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
<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 approach
here is a replication of the above code via 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>
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.