Здравствуйте, Аноним, Вы писали:
А>Не ожидал что так проблемно работать с image в WPF
А>вот вопросы которые решаются не так очевидно:
А>- копирование изображения из Clipboard в UI элемент Image на форме
А>- сохранение картинки из UI Image в файл
А>- загрузка изображения в UI Image из файла без последующей блокировки файла на запись
А>Помогите пожалуйста с кодом
Особых проблем не ощутил
<Window x:Class="WpfImage.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Height="600"
Width="800">
<Window.CommandBindings>
<CommandBinding Command="{x:Static ApplicationCommands.Copy}"
Executed="Copy_CommandBinding_Executed" />
<CommandBinding Command="{x:Static ApplicationCommands.Paste}"
Executed="Paste_CommandBinding_Executed"
CanExecute="CommandBinding_CanExecute_1" />
</Window.CommandBindings>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="File">
<MenuItem Header="Open"
Click="MenuItem_Click" />
<MenuItem Header="Save"
Click="MenuItem_Click_1" />
</MenuItem>
<MenuItem Header="Edit">
<MenuItem Header="Copy"
Command="{x:Static ApplicationCommands.Copy}" />
<MenuItem Header="Paste"
Command="{x:Static ApplicationCommands.Paste}" />
</MenuItem>
</Menu>
<Image Name="image"></Image>
</DockPanel>
</Window>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Images|*.bmp;*.jpg;*.png;*.gif;*.tif";
if (ofd.ShowDialog() == true)
{
FileInfo finf = new FileInfo(ofd.FileName);
using (FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read))
{
BitmapDecoder decoder = BitmapDecoder.Create(fs, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
if (decoder == null)
return ;
BitmapSource bitmapSource = decoder.Frames[0];
image.Source = bitmapSource;
}
}
}
private void MenuItem_Click_1(object sender, RoutedEventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Bitmap image|*.bmp;|Jpeg image|*.jpeg";
if (sfd.ShowDialog() == true && image.Source !=null)
{
FileInfo finf = new FileInfo(sfd.FileName);
FileStream fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write);
string ext = finf.Extension.ToLower();
BitmapEncoder bmpEncoder = null;
if (ext == ".bmp")
{
bmpEncoder = new BmpBitmapEncoder();
}
else if (ext == ".jpeg")
{
bmpEncoder = new JpegBitmapEncoder();
}
else if (ext == ".gif")
{
//Аналогично
}
bmpEncoder.Frames.Add(BitmapFrame.Create( image.Source as BitmapSource)) ;
bmpEncoder.Save(fs);
fs.Close();
}
}
private void Paste_CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
IDataObject dataObject = Clipboard.GetDataObject();
object bitmap = dataObject.GetData(DataFormats.Bitmap);
BitmapSource interopBitmap = bitmap as BitmapSource;
if (interopBitmap!=null)
image.Source = interopBitmap;
}
private void Copy_CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
if (image.Source != null)
Clipboard.SetImage(image.Source as BitmapSource);
}
private void CommandBinding_CanExecute_1(object sender, CanExecuteRoutedEventArgs e)
{
IDataObject dataObject = Clipboard.GetDataObject();
object bitmap = dataObject.GetData(DataFormats.Bitmap);
e.CanExecute = bitmap != null;
}
}