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; }
'WPF' 카테고리의 다른 글
[NavigationService / PageFunction] 페이지 이동 (2) | 2010.08.23 |
---|---|
[Canvas] 캔버스안의 컨트롤을 캔버스 가운데 띄우기 (0) | 2010.08.03 |
[DataGridHyperlinkColumn , EventSetter ] DataGridHyperlink 이벤트 처리 (0) | 2010.08.03 |
[WPF ReportViewer] WPF WinForm 리포트뷰 사용하기 (0) | 2010.08.02 |
[WPF Style/FindResource ] 비하인드 코드에서 스타일 먹히기 (0) | 2010.07.30 |
[MSDN]WPF/Silverlight데이터 바인딩 방법 항목 (0) | 2010.07.15 |
[DataGrid/Element Property Binding] 선택한 행 정보받아오기 (0) | 2010.07.09 |
[OneTime, OneWayToSource, TwoWay]바인딩 속성 (0) | 2010.07.08 |
Microsoft Silverlight Media Framework (0) | 2010.07.08 |
[CollectionViewSource] DataGird 사용하기 (0) | 2010.07.07 |