Ver código fonte

конвертация картинки в массив байт и обратно

fly9024 11 meses atrás
pai
commit
8531402cac

+ 2 - 1
MyWpfApp/MainWindow.xaml

@@ -25,7 +25,8 @@
             <RowDefinition Height="40"/>
         </Grid.RowDefinitions>
         <TextBlock Text="Типа баннер" Grid.Column="0" Grid.Row="1"/>
-        <Image Source="Resources/logo.png" Grid.Column="0" Grid.Row="0" />
+        <Image Source="{Binding ImageSource}" Grid.Column="0" Grid.Row="0" />
+        <Button Content="Выбрать изображение" Grid.Column="0" Grid.Row="1" Click="Button_Click"/>
         <TextBlock Text="Типа заголовок" Grid.Column="1" Grid.Row="0" Style="{StaticResource Zagolovok}"/>
         <TextBlock Text="Типа футер" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" TextAlignment="Center"/>
         <Frame Name="FrmMain" Grid.Column="1" Grid.Row="1" NavigationUIVisibility="Hidden"/>

+ 45 - 0
MyWpfApp/MainWindow.xaml.cs

@@ -1,5 +1,7 @@
 using System;
 using System.Collections.Generic;
+using System.Drawing;
+using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -12,7 +14,10 @@ using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using System.Windows.Navigation;
 using System.Windows.Shapes;
+using Microsoft.Win32;
+using MyWpfApp.models;
 using MyWpfApp.Pages;
+using static System.Net.Mime.MediaTypeNames;
 
 namespace MyWpfApp
 {
@@ -21,12 +26,52 @@ namespace MyWpfApp
     /// </summary>
     public partial class MainWindow : Window
     {
+        ImageBindings imageBindings = new ImageBindings();
         public MainWindow()
         {
+            
+            DataContext = imageBindings;
+
+           
             InitializeComponent();
+            
             FrmMain.Navigate(new Auth());
             Globals.MyGlobalFrame = FrmMain;
             Globals.MyGlobalConnection = new Smerdova_PPEntities();
+            
+
+        }
+
+        private void Button_Click(object sender, RoutedEventArgs e)
+        {
+            OpenFileDialog OFD = new OpenFileDialog();
+            OFD.Filter = "Все файлы | *.*; | Изображения |*.jpg;*.png;";
+            bool? success = OFD.ShowDialog();
+
+          
+            ImageConverter imageConverter = new ImageConverter();
+
+            //создаем объект изображения по известному пути к картинке
+            System.Drawing.Image image = System.Drawing.Image.FromFile(OFD.FileName);
+
+            //преобразуем картинку в массив байт
+            byte[] bytes = (byte[])imageConverter.ConvertTo(image, typeof(byte[]));
+
+
+            //преобразуем массив байт в BitmapImage (т.к. его можно помещать в ImageSource)          
+            using (MemoryStream stream = new MemoryStream(bytes))
+            {
+                var BMimage = new BitmapImage();
+                BMimage.BeginInit();
+                BMimage.CacheOption = BitmapCacheOption.OnLoad;
+                BMimage.StreamSource = stream;
+                BMimage.EndInit();
+                imageBindings.ImageSource = BMimage;
+            }
+            
+
+            imageBindings.ImgPath = OFD.FileName;
+            MessageBox.Show(imageBindings.ImgPath);
         }
     }
 }

+ 2 - 0
MyWpfApp/MyWpfApp.csproj

@@ -44,6 +44,7 @@
     <Reference Include="System" />
     <Reference Include="System.ComponentModel.DataAnnotations" />
     <Reference Include="System.Data" />
+    <Reference Include="System.Drawing" />
     <Reference Include="System.Runtime.Serialization" />
     <Reference Include="System.Security" />
     <Reference Include="System.Xml" />
@@ -72,6 +73,7 @@
       <DesignTime>True</DesignTime>
       <DependentUpon>Model1.tt</DependentUpon>
     </Compile>
+    <Compile Include="models\ImageBindings.cs" />
     <Compile Include="models\UserListModel.cs" />
     <Compile Include="Pages\PageForBindings.xaml.cs">
       <DependentUpon>PageForBindings.xaml</DependentUpon>

BIN
MyWpfApp/Resources/logo2.png


+ 35 - 0
MyWpfApp/models/ImageBindings.cs

@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.ComponentModel;
+using System.Windows.Media;
+
+namespace MyWpfApp.models
+{
+    internal class ImageBindings:INotifyPropertyChanged
+    {
+        string path; 
+        public string ImgPath //для загрузки картинки при известном пути
+        { get => path; set
+            {
+                path = value;
+                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(ImgPath)));
+            }
+        }
+
+        public ImageSource ImageSource //для загрузки картинки из массива байт
+        { get => imageSource; set
+            {
+                imageSource = value;
+                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(ImageSource)));
+            }
+         }
+
+        ImageSource imageSource;
+
+
+        public event PropertyChangedEventHandler PropertyChanged;
+    }
+}