wpf看图软件代码
时间: 2023-08-06 22:26:21 浏览: 150
WPF 看图软件的代码比较复杂,需要涉及到图像处理、界面设计、文件操作等多个方面。以下是一个简单的 WPF 看图软件的代码示例,供参考:
MainWindow.xaml:
```xaml
<Window x:Class="ImageViewer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Image Viewer" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Menu>
<MenuItem Header="_File">
<MenuItem Header="_Open" Click="MenuItemOpen_Click"/>
<MenuItem Header="_Save" Click="MenuItemSave_Click"/>
<Separator/>
<MenuItem Header="_Exit" Click="MenuItemExit_Click"/>
</MenuItem>
<MenuItem Header="_View">
<MenuItem Header="_Fit to Window" IsCheckable="True" Click="MenuItemFitToWindow_Click"/>
<Separator/>
<MenuItem Header="_Zoom In" Click="MenuItemZoomIn_Click"/>
<MenuItem Header="_Zoom Out" Click="MenuItemZoomOut_Click"/>
<Separator/>
<MenuItem Header="_Actual Size" Click="MenuItemActualSize_Click"/>
</MenuItem>
<MenuItem Header="_Help">
<MenuItem Header="_About" Click="MenuItemAbout_Click"/>
</MenuItem>
</Menu>
<StatusBar Grid.Row="1" Height="20"/>
<ScrollViewer Grid.Row="1" Name="ImageScrollViewer" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<Image Name="ImageView" Stretch="None"/>
</ScrollViewer>
</Grid>
</Window>
```
MainWindow.xaml.cs:
```csharp
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace ImageViewer
{
public partial class MainWindow : Window
{
private BitmapImage _bitmap;
private double _zoomFactor = 1.0;
public MainWindow()
{
InitializeComponent();
}
private void MenuItemOpen_Click(object sender, RoutedEventArgs e)
{
var openFileDialog = new Microsoft.Win32.OpenFileDialog();
openFileDialog.Filter = "Image Files (*.bmp, *.jpg, *.png)|*.bmp;*.jpg;*.png";
if (openFileDialog.ShowDialog() == true)
{
try
{
_bitmap = new BitmapImage(new Uri(openFileDialog.FileName));
ImageView.Source = _bitmap;
UpdateZoomFactor();
StatusText.Content = openFileDialog.FileName;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private void MenuItemSave_Click(object sender, RoutedEventArgs e)
{
if (_bitmap != null)
{
var saveFileDialog = new Microsoft.Win32.SaveFileDialog();
saveFileDialog.Filter = "BMP Files (*.bmp)|*.bmp|JPEG Files (*.jpg)|*.jpg|PNG Files (*.png)|*.png";
if (saveFileDialog.ShowDialog() == true)
{
try
{
var encoder = GetBitmapEncoder(saveFileDialog.FileName);
encoder.Frames.Add(BitmapFrame.Create(_bitmap));
using (var stream = new FileStream(saveFileDialog.FileName, FileMode.Create))
{
encoder.Save(stream);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
private BitmapEncoder GetBitmapEncoder(string fileName)
{
switch (Path.GetExtension(fileName).ToUpper())
{
case ".BMP":
return new BmpBitmapEncoder();
case ".JPG":
return new JpegBitmapEncoder();
case ".PNG":
return new PngBitmapEncoder();
default:
throw new NotSupportedException("Unsupported file format.");
}
}
private void MenuItemExit_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void MenuItemFitToWindow_Click(object sender, RoutedEventArgs e)
{
if (_bitmap != null)
{
if (MenuItemFitToWindow.IsChecked)
{
ImageView.Stretch = Stretch.Uniform;
}
else
{
ImageView.Stretch = Stretch.None;
}
}
}
private void MenuItemZoomIn_Click(object sender, RoutedEventArgs e)
{
if (_bitmap != null)
{
_zoomFactor *= 1.1;
UpdateZoomFactor();
}
}
private void MenuItemZoomOut_Click(object sender, RoutedEventArgs e)
{
if (_bitmap != null)
{
_zoomFactor /= 1.1;
UpdateZoomFactor();
}
}
private void MenuItemActualSize_Click(object sender, RoutedEventArgs e)
{
if (_bitmap != null)
{
_zoomFactor = 1.0;
UpdateZoomFactor();
}
}
private void UpdateZoomFactor()
{
if (_bitmap != null)
{
ImageView.Width = _bitmap.PixelWidth * _zoomFactor;
ImageView.Height = _bitmap.PixelHeight * _zoomFactor;
}
}
private void MenuItemAbout_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Image Viewer\r\n(C) 2021 WPF Sample", "About", MessageBoxButton.OK, MessageBoxImage.Information);
}
private void ImageView_MouseWheel(object sender, MouseWheelEventArgs e)
{
if (_bitmap != null)
{
if (Keyboard.Modifiers == ModifierKeys.Control)
{
if (e.Delta > 0)
{
_zoomFactor *= 1.1;
}
else
{
_zoomFactor /= 1.1;
}
e.Handled = true;
UpdateZoomFactor();
}
}
}
}
}
```
这个简单的 WPF 看图软件实现了打开和保存图像文件、缩放、显示当前图像文件路径等功能。但是它还有很多不足之处,例如没有实现图像的旋转、翻转、裁剪等功能,也没有实现图像的拖放、缩略图、分页浏览等高级功能。如果需要实现更复杂的功能,就需要更多的代码和技术。
阅读全文