MSDN.WhiteKnight - Stack Overflow answers
Ответ на "Изменение имени картинки"
Answer 961418
Проблема в том, что при таком способе создания BitmapImage исходный файл открывается монопольно и остается открытым так неопределенное время (видимо, пока его не съест сборщик мусора), так как метода закрытия источника у этого класса нет. Вам нужно вместо этого реализовать загрузку изображения в промежуточный буфер в памяти, тогда вы сможете перемещать его как угодно. Например, это можно реализовать так:
<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Title="MainWindow" Height="400" Width="600"> <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > <TextBox HorizontalAlignment="Stretch" Height="60" Margin="20,20,170,20" x:Name="tbFilename" TextWrapping="Wrap" Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" /> <Button Content="Save" HorizontalAlignment="Right" Height="60" Margin="20,20,20,20" VerticalAlignment="Top" Width="120" Click="Save_Click" IsEnabled="{Binding Path=HasChanges}"/> <Image HorizontalAlignment="Stretch" Height="226" Margin="20,100,20,20" VerticalAlignment="Stretch" Source="{Binding Path=Image}"/> </Grid> </Window>
using System; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Reflection; using System.ComponentModel; namespace WpfApp1 { public partial class MainWindow : Window { ImageFile imagefile; public MainWindow() { InitializeComponent(); var directory = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); directory = System.IO.Path.Combine(directory, "images"); imagefile = new ImageFile(directory, "test"); this.DataContext = imagefile; } private void Save_Click(object sender, RoutedEventArgs e) { imagefile.Save(); } } public class ImageFile: INotifyPropertyChanged { string dir; string source_name; BitmapImage img; string new_name; static BitmapImage LoadImage(string path) { byte[] data = System.IO.File.ReadAllBytes(path); var ms = new System.IO.MemoryStream(data); BitmapImage im = new BitmapImage(); im.BeginInit(); im.StreamSource = ms; im.EndInit(); return im; } public ImageFile(string d,string name) { dir = d; source_name = name; var source_path = System.IO.Path.Combine(dir, source_name+".png"); img = LoadImage(source_path); new_name = name; } public string Name { get { return new_name; } set { new_name = value; OnPropertyChanged("Name"); OnPropertyChanged("HasChanges"); } } public ImageSource Image { get { return img; } } public bool HasChanges { get { return new_name != source_name; } } public void Save() { if (new_name != source_name) { var source_path = System.IO.Path.Combine(dir, source_name + ".png"); var dest_path = System.IO.Path.Combine(dir, new_name + ".png"); System.IO.File.Move(source_path, dest_path); source_name = new_name; OnPropertyChanged("HasChanges"); } } public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged(string name) { var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(name)); } } }
Content is retrieved from StackExchange API.
Auto-generated by ruso-archive tools.