使用 Caliburn.Micro 以编程方式修改图像

本文关键字:方式 修改 图像 编程 Caliburn Micro 使用 | 更新日期: 2023-09-27 18:33:20

我想将效果/滤镜应用于我的视图中显示的图像,内存中(不保存图像文件)。

我正在使用Caliburn.Micro作为MVVM框架。

我有这样的看法:

<UserControl x:Class="TestApplication.Views.MainView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="0.3*" />
        </Grid.ColumnDefinitions>
        <Image
            Grid.Column="0"
            Stretch="Uniform"
            Source="{Binding PreviewUrl}"
            Margin="5"
            x:Name="Preview" />
        <ContentControl
            Grid.Column="1"
            Margin="5"
            x:Name="ImageManagement" />
    </Grid>
</UserControl>

而这个视图模型:

namespace TestApplication.ViewModels
{
    using System;
    using System.ComponentModel.Composition;
    using System.Linq;
    using Caliburn.Micro;
    using ImageProcessor;
    using ImageProcessor.Plugins.Popart;
    using PropertyChanged;
    using TestApplication.Events;
    using TestApplication.Models;
    [ImplementPropertyChanged]
    [Export(typeof(MainViewModel))]
    public class MainViewModel : PropertyChangedBase, IHandle<FileSelectedEvent>, IHandle<FilterChangedEvent>
    {
        private readonly IEventAggregator events;
        private bool hasCustomImage = false;
        [ImportingConstructor]
        public MainViewModel(IEventAggregator events)
        {
            this.events = events;
            this.events.Subscribe(this);
            this.ImageManagement = new ImageManagementViewModel(events);
            this.ImageManagement.SettingsEnabled = false;
            this.PreviewUrl = "pack://application:,,,/Resources/placeholder.jpg";
        }
        public ImageManagementViewModel ImageManagement { get; set; }
        public String PreviewUrl { get; set; }
        public void Handle(FilterChangedEvent message)
        {
            this.ApplyFilter(new Filter(message));
        }
        public void Handle(FileSelectedEvent message)
        {
            this.PreviewUrl = message.FilePath;
            this.hasCustomImage = true;
            this.ImageManagement.SettingsEnabled = true;
            this.ApplyFilter(Filter.Default);
        }
        private void ApplyFilter(Filter filter)
        {
            if (this.hasCustomImage)
            {
                using (ImageFactory factory = new ImageFactory())
                {
                    factory.Load(this.PreviewUrl);
                    // TODO: apply a filter using factory
                    // TODO: use this processed image
                }
            }
        }
    }
}

在 ApplyFilter 方法中,我需要将处理后的System.Drawing.Image绑定到视图的图像源,但我完全不知道该怎么做(我还不熟悉所有 wpf 方面)。

使用 Caliburn.Micro 以编程方式修改图像

使用@Charleh的评论和其他各种帖子,我已经能够找到它应该如何工作:

<Image
    Grid.Column="0"
    Stretch="Uniform"
    Margin="5"
    Source="{Binding Preview}" />

public class MainViewModel : PropertyChangedBase, IHandle<FileSelectedEvent>, IHandle<WarholFilterChangedEvent>
{
    private readonly IEventAggregator events;
    private bool hasCustomImage = false;
    private string sourceUrl;
    [ImportingConstructor]
    public MainViewModel(IEventAggregator events)
    {
        this.events = events;
        this.events.Subscribe(this);
        this.ImageManagement = new ImageManagementViewModel(events);
        this.ImageManagement.WarholSettingsEnabled = false;
        this.Preview = new BitmapImage(new Uri("pack://application:,,,/Resources/placeholder.jpg"));
    }
    public ImageManagementViewModel ImageManagement { get; set; }
    public BitmapImage Preview { get; set; }
    public void Handle(WarholFilterChangedEvent message)
    {
        this.ApplyFilter(new WarholFilter(message));
    }
    public void Handle(FileSelectedEvent message)
    {
        this.sourceUrl = message.FilePath;
        this.hasCustomImage = true;
        this.ImageManagement.WarholSettingsEnabled = true;
        this.ApplyFilter(WarholFilter.Default);
    }
    private void ApplyFilter(WarholFilter filter)
    {
        if (this.hasCustomImage)
        {
            using (ImageFactory factory = new ImageFactory())
            {
                factory.Load(this.sourceUrl);
                PolychromaticParameters parameters = new PolychromaticParameters();
                parameters.Number = filter.ColorsNumber;
                parameters.Colors = filter.Colors.Select(c => System.Drawing.Color.FromArgb(c.R, c.G, c.B)).ToArray();
                parameters.Thresholds = filter.Thresholds.ToArray();
                factory.Polychromatic(parameters);
                BitmapImage img = new BitmapImage();
                using (MemoryStream str = new MemoryStream())
                {
                    img.BeginInit();
                    img.CacheOption = BitmapCacheOption.OnLoad;
                    factory.Save(str);
                    str.Seek(0, SeekOrigin.Begin);
                    img.StreamSource = str;
                    img.EndInit();
                }
                this.Preview = img;
            }
        }
    }
}