xaml文件代码
<Window x:Class="ImageShowDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <s:Boolean x:Key="True">True</s:Boolean> <s:Boolean x:Key="False">False</s:Boolean> </Window.Resources> <!--只有鼠标在Image控件上时,才会触发MouseWheel事件,因为Grid.Background默认为null 将Background设为不为null时,即可在Grid范围触发MouseWheel事件--> <Grid MouseWheel="Grid_MouseWheel" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="9*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <!--Image无Background属性--> <Image x:Name="image" Width="auto" Height="auto" Source="{Binding buffer}" HorizontalAlignment="Center" VerticalAlignment="Center"/> <Border Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Blue" BorderThickness="1"> <TextBlock Text="测试测试测试测试测试测试测试测试测试测试测试" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> </Grid> </Window>
对应的xmal.cs文件代码:
using Microsoft.Win32; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace ImageShowDemo { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public byte[] buffer; public MainWindow() { InitializeComponent(); OpenFileDialog dialog = new OpenFileDialog() { Filter = string.Format("{0}|*.jpg;*.png;*.jpeg", "图片文件") }; if ((bool)dialog.ShowDialog()) { buffer = FileToByteArray(dialog.FileName); this.image.Source = ByteArrayToBitmapImage(buffer); } } #region WPF Image控件 Source,byte[],BitmapImage相互转换 /// <summary> /// 文件转换为byte[] /// </summary> /// <param name="fileName"></param> /// <returns></returns> private byte[] FileToByteArray(string filePath) { FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); try { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, (int)fs.Length); return buffer; } catch (Exception ex) { Console.WriteLine(ex.Message); return null; } finally { if (fs != null) fs.Close(); } } /// <summary> /// byte[]转换为BitmapImage /// </summary> /// <param name="byteArray"></param> /// <returns></returns> private BitmapImage ByteArrayToBitmapImage(byte[] byteArray) { BitmapImage bmp = null; try { bmp = new BitmapImage(); bmp.BeginInit(); bmp.StreamSource = new MemoryStream(byteArray); bmp.EndInit(); } catch (Exception ex) { Console.WriteLine(ex.Message); bmp = null; } return bmp; } /// <summary> /// BitmapImage转换为byte[] /// </summary> /// <param name="bmp"></param> /// <returns></returns> private byte[] BitmapImageToByteArray(BitmapImage bmp) { byte[] byteArray = null; Stream sMarket = bmp.StreamSource; try { if (sMarket != null && sMarket.Length > 0) { // Position位于Stream的末尾,需手动置为0,否则下面读取到的长度为0 sMarket.Position = 0; using (BinaryReader br = new BinaryReader(sMarket)) { byteArray = br.ReadBytes((int)sMarket.Length); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { sMarket.Close(); } return byteArray; } #endregion /// <summary> /// 通过滚动鼠标滚轮对图片进行缩放显示 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Grid_MouseWheel(object sender, MouseWheelEventArgs e) { Matrix m = image.RenderTransform.Value; Point p = new Point(image.ActualWidth / 2, image.ActualHeight / 2); if (e.Delta > 0) { m.ScaleAtPrepend(1.1, 1.1, p.X, p.Y); } else { m.ScaleAtPrepend(1 / 1.1, 1 / 1.1, p.X, p.Y); } image.RenderTransform = new MatrixTransform(m); } } }