图像上的共享冲突 查看和编辑 WPF



我正在研究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>
/

/视图模型代码:

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

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

最新更新