Adding image from source property of image control(WPF) at Access Database

رضا جافری 1,296 Reputation points
2021-01-13T13:57:23.793+00:00

Hi to all
First and foremost, I apologize for my grammatical errors; my first language is Persian (Iran).
This is tested solution

XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
819 questions
0 comments No comments
{count} votes

Accepted answer
  1. رضا جافری 1,296 Reputation points
    2021-01-13T13:58:28.263+00:00

    I found the solution which is working

      //Global field  
      BitmapImage BM;  
      //Convert image to bytes by following method  
      private static byte[] ImageToBytes(BitmapImage image)  
      {  
             byte[] Data;  
             JpegBitmapEncoder JpegEncoder = new JpegBitmapEncoder();  
             JpegEncoder.Frames.Add(BitmapFrame.Create(image));  
             using (System.IO.MemoryStream MS = new System.IO.MemoryStream())  
             {  
                 JpegEncoder.Save(MS);  
                 Data = MS.ToArray();  
             }  
             return Data;  
      }  
      //Convert bytes to image by following method  
      private BitmapImage GetImageFromBytes(byte[] bytes)  
      {  
             System.IO.MemoryStream Stream = new System.IO.MemoryStream();  
             Stream.Write(bytes, 0, bytes.Length);  
             Stream.Position = 0;  
             System.Drawing.Image img = System.Drawing.Image.FromStream(Stream);  
             BitmapImage bitImage = new BitmapImage();  
             bitImage.BeginInit();  
             System.IO.MemoryStream MS = new System.IO.MemoryStream();  
             img.Save(MS, System.Drawing.Imaging.ImageFormat.Jpeg);  
             MS.Seek(0, System.IO.SeekOrigin.Begin);  
             bitImage.StreamSource = MS;  
             bitImage.EndInit();  
             return bitImage;  
      }  
      //Insert image in WPF control image  
      private void UploadButton_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)  
      {  
             if(OpenFileDialog.ShowDialog()==System.Windows.Forms.DialogResult.OK)  
             {  
              var IMG = System.Drawing.Image.FromFile(OpenFileDialog.FileName);  
              BM = new BitmapImage(new Uri(OpenFileDialog.FileName));  
              BitmapImage BitMapImage = new BitmapImage();  
              BitMapImage.BeginInit();  
              System.IO.MemoryStream MemoryStream = new System.IO.MemoryStream();  
              IMG.Save(MemoryStream, System.Drawing.Imaging.ImageFormat.Bmp);  
              MemoryStream.Seek(0, System.IO.SeekOrigin.Begin);  
              BitMapImage.StreamSource = MemoryStream;  
              BitMapImage.EndInit();  
              MemberImage.Source = BitMapImage;  
              OpenFileDialog.Dispose();  
             }  
      }  
      //Now Insert Image and Name by The following code  
      private void Add_Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)  
      {  
             OleDbConnect.Open();  
             byte[] Image_Bytes = ImageToBytes(BM);  
             OleDbParameter Parameter = new OleDbParameter();  
             Parameter.OleDbType = OleDbType.Binary;  
             Parameter.ParameterName = "Image";  
             Parameter.Value = Image_Bytes;  
             OleDbCommand OleDbCommand_Insert = new OleDbCommand("Insert Into         
             MemberTable(FirstName,MemberImage)values(" +  
             "'" + FirstName_TextBox.Text + "',[@](/users/na/?userId=120b56f4-bffd-0003-0000-000000000000))", OleDbConnect);  
             OleDbCommand_Insert.Parameters.Add(Parameter);  
             OleDbCommand_Insert.ExecuteScalar();  
             OleDbConnect.Close();  
      }  
      //Now retrieve Image and Name by The following code from DataBase  
      private void Search_Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)  
      {  
             OleDbCommand OleDCmd = new OleDbCommand("Select  * From MemberTable Where FirstName='"        
             + FirstName_TextBox.Text + "'",OleDbConnect);  
             OleDCmd.CommandType = System.Data.CommandType.Text;  
             FirstName_TextBox.Text = null;  
             OleDbConnect.Open();  
             OleDbDataReader DataReader = OleDCmd.ExecuteReader();  
             while (DataReader.Read())  
             {  
              FirstName_TextBox.Text = DataReader[0].ToString();  
              BitmapImage BMP = GetImageFromBytes((byte[])DataReader[11]);  
              MemberImage.Source = BMP;  
             }  
      }  
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.