c# - 在图像查看和编辑 WPF 上共享冲突

标签 c# wpf xaml file-io mvvm

我正在处理 MVVMWPF应用。我有一个场景,我必须编辑查看图像并保存它。

我用过RadCarousel其中使用ContextMenu右键单击我正在使用 mspaint 编辑图像.当我尝试保存编辑后的图像时,我得到“Sharing violation error on path”。

图像位于共享文件夹中。

//XAML 代码:

    <telerik:RadCarousel x:Name="MarketSeriesCarousel" 
                             HorizontalAlignment="Stretch"
                             VerticalAlignment="Stretch" 
                             ItemsSource="{Binding Path=MarketSeriesImageList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" 
                             SelectedItem="{Binding SelectedMarketSeriesImage, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" 
                             Background="Transparent" 
                             HorizontalScrollBarVisibility="Auto" 
                             ScrollViewer.CanContentScroll="True" 
                             ScrollViewer.VerticalScrollBarVisibility="Auto"                                                                 
                             telerik:StyleManager.Theme="Windows8" 
                             Focusable="True" 
                             PropertyChanged="MarketSeriesCarousel_PropertyChanged">
     <telerik:RadCarousel.ContextMenu>
        <ContextMenu >
            <MenuItem Header="Edit" Command="{Binding EditImage}" CommandParameter="{Binding }"/>     
            <MenuItem Header="MetaData" IsEnabled="False"/>  
            <MenuItem Header="Delete" Command="{Binding DeleteImage}"/>                         
        </ContextMenu>

    </telerik:RadCarousel.ContextMenu>     

    <telerik:RadCarousel.ItemsPanel>
        <ItemsPanelTemplate>
            <telerik:RadCarouselPanel Path="{StaticResource path}" 
                                      telerik:StyleManager.Theme="Windows8" >
            </telerik:RadCarouselPanel>
        </ItemsPanelTemplate>
    </telerik:RadCarousel.ItemsPanel>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDoubleClick">
            <i:InvokeCommandAction Command="{Binding ViewSeriesImage}" />
        </i:EventTrigger>

    </i:Interaction.Triggers>
</telerik:RadCarousel>

// View 模型代码:
/// <summary>
        /// Edit image viewer.
        /// </summary>
        private void EditImageViewer()
        {
            try
            {

                ProcessStartInfo startInfo = new ProcessStartInfo(ImagePath);
                startInfo.Verb = "edit";
                Process proc = Process.Start(startInfo);

            }
            catch (Exception ex)
            {
                // Error handling.

                throw new ApplicationException(ex.Message);
            }
        }

我可以实现这一目标的任何可能方式?或图像编辑的任何替代方案。

但我需要 mspaint 中的所有功能.

最佳答案

您收到此错误是因为您的 wpf 应用程序保持图像文件锁定,因为您实际上是在引用它。
要解决此问题,请创建同一文件的内存位图图像。我使用以下代码也使用 mvvm :

public BitmapImage Image
    {
        get
        {
            if (!image_retrieved) getImageAsync();
            return _Image;
        }
    }

private async Task getImageAsync()
    {
        image_retrieved = true;
        _Image = await ImageFactory.CreateImageAsync(ImageFullPath).ConfigureAwait(true);
        this.OnPropertyChanged(() => Image);
    }

public static async Task<BitmapImage> CreateImageAsync(string filename)
    {
        if (!string.IsNullOrEmpty(filename) && File.Exists(filename))
        {
            try
            {
                byte[] buffer = await ReadAllFileAsync(filename).ConfigureAwait(false);
                System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);
                BitmapImage image = new BitmapImage();
                image.BeginInit();
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.StreamSource = ms;
                image.EndInit();
                image.Freeze();
                return image;
            }
            catch
            {
                return null;
            }
        }
        else return null;
    }

static async Task<byte[]> ReadAllFileAsync(string filename)
    {
        try
        {
            using (var file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
            {
                byte[] buff = new byte[file.Length];
                await file.ReadAsync(buff, 0, (int) file.Length).ConfigureAwait(false);
                return buff;
            }
        }
        catch
        {
            return null;
        }
    }

然后更改您的绑定(bind)以绑定(bind)到 Image 属性。
如果您不需要在您的 gui 线程中执行此操作,它会异步创建图像。

关于c# - 在图像查看和编辑 WPF 上共享冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29097171/

相关文章:

c# - WPF ListView 星号 ("*") 大小

xaml - 如何将 View 与ViewModel或ViewModel的多个DataTemplates关联?

c# - 使用 Entity Framework 编辑 datagridview 中的列标题标题

c# - 如何使用MVVM媒体元素移至下一首歌曲

wpf - FrameworkElementFactory 必须位于此操作的密封模板中

wpf - 适用于 Win7 和 Win8 主题的 WPF 拆分按钮

c# - 使用 iTextSharp 创建的简单 PDF 无法用 Acrobat Reader 打开?

c# - 使 StructLayout 在类上工作,就像它在结构上工作一样

c# - 从字符串中获取总数 'objects'

Python .NET - 使用 XAML/WPF 进行 GUI 开发以及使用 DataTemplate 进行数据绑定(bind)