加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 删除绑定到控件的图像

发布时间:2020-12-15 23:51:55 所属栏目:百科 来源:网络整理
导读:我正在编写一个 Image Manager WPF应用程序.我有一个带有以下ItemsTemplate的ListBox: Grid x:Name="grid" Width="150" Height="150" Background="{x:Null}" Grid.RowDefinitions RowDefinition Height="*"/ RowDefinition Height="27.45"/ /Grid.RowDefini
我正在编写一个 Image Manager WPF应用程序.我有一个带有以下ItemsTemplate的ListBox:

<Grid x:Name="grid" Width="150" Height="150" Background="{x:Null}">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="27.45"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="150"/>
            </Grid.ColumnDefinitions>
            <Border Margin="5,5,5.745" Grid.RowSpan="2" Background="#FF828282" BorderBrush="{DynamicResource ListBorder}" CornerRadius="5,5" BorderThickness="1,1,2,2" x:Name="border">
                <Grid>
                    <Viewbox Margin="0,21.705">
                        <Image Width="Auto" Height="Auto" x:Name="picture" Source="{Binding Path=FullName}" />
                    </Viewbox>
                    <TextBlock Height="Auto" Text="{Binding Path=Name}" TextWrapping="Wrap" x:Name="PictureText" HorizontalAlignment="Left" Margin="70,0" VerticalAlignment="Bottom" />
                </Grid>
            </Border>
        </Grid>

请注意,“Image”控件绑定到“FullName”属性,该属性是表示JPG的绝对路径的字符串.

一些应用程序功能要求我更改JPG文件(移动,重命名或删除).当我尝试这样做(当前尝试移动文件)时,我收到一个IOException:“进程无法访问该文件,因为它正被另一个进程使用.”锁定文件的进程是我的WPF应用程序.

我做了一些在线搜索,发现了一些帖子,表明图片特别是放弃了他们的资源.我尝试过以下方法:

>将ListBox.Source设置为null
>之前增加10秒的等待时间
试图移动.
>发布GC.Collect().
>将操作移至另一个
线.

我还能尝试什么?我想在ItemsTemplate中找到对Image对象的引用并尝试处理Image,但我无法弄清楚如何获取引用.

我读到的一个可能的解决方案是创建图像的副本而不是实际的图像,但由于绑定是文件名而不是实际的图像我不知道我是否可以使这个工作.

任何帮助或建议将非常感激.

解决方法

我的 Intuipic应用程序也允许用户删除图像.我不得不写 this converter来实现它.相关代码:

//create new stream and create bitmap frame
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new FileStream(path,FileMode.Open,FileAccess.Read);
bitmapImage.DecodePixelWidth = (int) _decodePixelWidth;
bitmapImage.DecodePixelHeight = (int) _decodePixelHeight;
//load the image now so we can immediately dispose of the stream
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();

//clean up the stream to avoid file access exceptions when attempting to delete images
bitmapImage.StreamSource.Dispose();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读