FileDialog로 로컬파일 선택해서 표시하기

       private void selectBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            
            // TODO: 사진선택.
           
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "Images";
            dlg.DefaultExt = ".jpg"; 
            dlg.Filter = "JPEG |*.jpg| GIF |*.gif"; 
            
            Nullable<bool> result = dlg.ShowDialog();
           
            if (result == true)
            {

                string selectedFileName = dlg.FileName;                

                BitmapImage bitmap = new BitmapImage();

                bitmap.BeginInit();

                bitmap.UriSource = new Uri(selectedFileName);

                bitmap.EndInit();

                ImgUserImage.Source = bitmap;

                
            }
        }

선택한 파일 메모리스트림으로 저장하기

        private void selectBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {

            // TODO: 사진선택.

            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "Images";
            dlg.DefaultExt = ".jpg";
            dlg.Filter = "JPEG |*.jpg| GIF |*.gif";

            Nullable<bool> result = dlg.ShowDialog();

            if (result == true)
            {

                string selectedFileName = dlg.FileName;

                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.UriSource = new Uri(selectedFileName);
                bitmap.EndInit();

                ImgUserImage.Source = bitmap;
                this.txtFileName.Text = selectedFileName;

                Int32 reads = 0;

                //임시 메모리스트림에 작성
                using (Stream st = dlg.OpenFile())
                {
                    using (MemoryStream output = new MemoryStream())
                    {
                        st.Position = 0;
                        Byte[] buffer = new Byte[256];

                        while (0 < (reads = st.Read(buffer, 0, buffer.Length)))
                        {
                            output.Write(buffer, 0, reads);
                        }
                        TempMemoryStream = output;
                        output.Flush();
                    } // in using
                } // out using
            }
        }

메모리스트림을 저장하기

pp.Personnel.Photo 은 System.Data.Linq.Binary 타입

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_Photo", DbType = "Image", UpdateCheck = UpdateCheck.Never)]
        public System.Data.Linq.Binary Photo
        {
            get
            {
                return this._Photo;
            }
            set
            {
                if ((this._Photo != value))
                {
                    this.OnPhotoChanging(value);
                    this.SendPropertyChanging();
                    this._Photo = value;
                    this.SendPropertyChanged("Photo");
                    this.OnPhotoChanged();
                }
            }
        }

데이터베이스에 저장하기 위한 변수에 담기
pp.Personnel.Photo = new System.Data.Linq.Binary(TempMemoryStream.GetBuffer());

System.Data.Linq.Binary 형식을
WPF Image 컨트롤에 바인딩 시키기

 

        /// <summary>
        /// 외부에서 이미지 데이터가 변경되었습니다. 이미지 바인딩 처리;
        /// </summary>
        public void UpdatePersonImage()
        {
            Contract.Data.Insa.ParentPerson pp = this.MainContentGrid.DataContext as Contract.Data.Insa.ParentPerson;
            if (pp.Personnel.Photo.Length > 0)
            {
                System.Data.Linq.Binary binaryFile = pp.Personnel.Photo;                 
       
                using( System.IO.MemoryStream ms = new System.IO.MemoryStream( binaryFile.ToArray()))
                {

                    System.Drawing.Bitmap bm = new System.Drawing.Bitmap(ms);
                    this.imgPerson.Source = GetBitmapSource(bm);
                }

            }
        }
        private System.Windows.Media.Imaging.BitmapSource GetBitmapSource(System.Drawing.Bitmap _image)
        {
            System.Drawing.Bitmap bitmap = _image;
            System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                bitmap.GetHbitmap(),
                IntPtr.Zero,
                Int32Rect.Empty,
                System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
            return bitmapSource;
        }

+ Recent posts