【问题标题】:Modifying an image from a Image tag in WPF从 WPF 中的 Image 标签修改图像
【发布时间】:2011-06-20 19:56:41
【问题描述】:

我有一个显示一堆图像的表单。我希望能够通过另一个可执行文件(如绘画)修改此图像,并在我退出时刷新图像(例如通过按钮)。

当我尝试将图像保存在绘画中时,它告诉我图像受到保护,这是由于表单以某种方式链接到图像。

有什么办法可以使这项工作?知道如何绕过这个问题吗?

谢谢。

编辑:这是我使用的结构(高度简化)

        <ListView Style="{StaticResource BaseListView}" x:Name="LstMoulures" Grid.Column="0" Background="#00000000" SelectionMode="Single" IsTextSearchEnabled="False" ScrollViewer.VerticalScrollBarVisibility="Auto">
           <ListView.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="White" Margin="1,1,1,1" BorderThickness="2,2,2,2" CornerRadius="4">
                        <StackPanel Orientation="Horizontal" Margin="5,0,0,5" Height="200">
                            <Image x:Name="ImgPicture" Width="220" Height="195" Margin="2,5,2,2" GotFocus="ImgPicture_GotFocus" MouseLeftButtonDown="ImgPicture_MouseLeftButtonDown" MouseEnter="ImgPicture_MouseEnter" MouseLeave="ImgPicture_MouseLeave">
                                <Image.Source>
                                    <BitmapImage UriSource="{Binding DessinSource}" CacheOption="OnLoad" />
                                </Image.Source>
                            </Image>
                         </StackPanel>
                    </Border>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

【问题讨论】:

    标签: c# wpf image readonly


    【解决方案1】:
    <Image>
        <Image.Source>
            <BitmapImage UriSource="test.jpg" CacheOption="OnLoad" />
        </Image.Source>
    </Image>
    

    您还可以读取文件并将其重写为MemoryStream 对象:

    MemoryStream ms = new MemoryStream();
    BitmapImage bi = new BitmapImage();
    
    byte[] bytArray = File.ReadAllBytes(@"test.jpg");
    ms.Write(bytArray, 0, bytArray.Length);
    ms.Position = 0;
    bi.BeginInit();
    bi.StreamSource = ms;
    bi.EndInit();
    image.Source = bi;
    

    要监控图像变化和刷新图像,您可以使用FileSystemWatcher

    【讨论】:

    • @navid:CacheOption 的含义是什么?您的回答并没有真正回答主要问题
    • here
    • 如果你使用CacheOption="OnLoad"所有对图像数据的请求都是从内存存储中填充的。
    • 第一个选项似乎对我有用。我只需要检查 FileSystemWatcher 的变化。
    • 所以我试过了,它适用于 Image CacheOption 的东西,当我使用文件系统观察器时,它确实检测到了变化,但现在我该怎么做才能“刷新”缓存?当我尝试执行 Lst.Items.Refresh 时,我得到一个线程异常。
    猜你喜欢
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 2013-03-09
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    相关资源
    最近更新 更多