Convert image files, loading and zoom display

Code xaml file

<The 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-: the System; = mscorlib Assembly " 
        the Title =" the MainWindow "the Height =" 350 "the Width =" 525 "> 
    <Window.Resources> 
        <S: Boolean X: Key =" True " > True </ S: Boolean> 
        <S: Boolean the X-: Key = "False"> False </ S: Boolean> 
    </Window.Resources> 
    ! <- only when the mouse over the Image control to trigger the event MouseWheel because Grid.Background defaults to null 
        the Background set is not null, to trigger events in the Grid MouseWheel range -> 
    <Grid = MouseWheel "Grid_MouseWheel" Background="Transparent">
        <Grid.RowDefinitions>
            <The RowDefinition the Height = "*. 9" />
            <The RowDefinition the Height = "*" /> 
        </Grid.RowDefinitions> 
        <- No Background Image Properties -!> 
        <Image X: the Name = "Image" the Width = "Auto" the Height = "Auto" the Source = "{the Binding } Buffer "the HorizontalAlignment =" Center "the VerticalAlignment =" Center "/> 
        <= the Grid.Row Border". 1 "= the HorizontalAlignment" the Stretch "the VerticalAlignment =" the Stretch "the BorderBrush =" Blue "BorderThickness =". 1 "> 
            <= the TextBlock the Text" test test test test test test test test test test test "the HorizontalAlignment =" Center "the VerticalAlignment =" Center "/> 
        </ Border> 
    </ the Grid> 
</ the Window>

  Xmal.cs corresponding file code:

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);
                = null BMP; 
            } 
            return bmp;
        }
        /// <Summary> 
        /// the BitmapImage converted to byte [] 
        /// </ Summary> 
        /// <param name = "BMP"> </ param> 
        /// <Returns> </ Returns> 
        Private byte [] BitmapImageToByteArray (the BitmapImage BMP) 
        { 
            byte [] = byteArray the null; 
            Stream sMarket = bmp.StreamSource; 
            the try 
            { 
                IF (! = null && sMarket.Length sMarket> 0) 
                { 
                    // Stream at the end of the Position, need to manually set to 0, or below the read length is 0 
                    sMarket.Position = 0; 
                    the using (the BinaryReader the BinaryReader br = new new (sMarket)) 
                    {
                        = br.ReadBytes byteArray The ((int) sMarket.Length); 
                    } 
                } 
            } 
            the catch (Exception EX) 
            { 
                Console.WriteLine (ex.Message); 
            } 
            the finally 
            { 
                sMarket.Close (); 
            } 
            return byteArray The; 
        } 
        #endregion 
      // / <Summary> 
        /// by rolling the mouse wheel to zoom the picture display 
        /// </ 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);
        }
    }
}

  

Guess you like

Origin www.cnblogs.com/youknowUL/p/11331643.html