【问题标题】:Modify image programmatically using Caliburn.Micro使用 Caliburn.Micro 以编程方式修改图像
【发布时间】:2015-02-03 23:20:08
【问题描述】:

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

我使用 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 的所有方面)。

【问题讨论】:

标签: c# wpf mvvm caliburn.micro data-binding


【解决方案1】:

使用@Charleh 的 cmets 和各种 other posts,我已经能够找到它应该如何工作:

<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;
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2010-09-29
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 2015-02-27
    • 2017-08-01
    • 2011-09-14
    • 2021-01-14
    相关资源
    最近更新 更多