1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
private void PathToPicture()
{
String destinationPath = "C:\\Images";
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document";
dlg.DefaultExt = ".jpg";
dlg.Filter = "JPEG|*.jpg|PNG|*.png|BITMAP|*.bmp|Tous les fichiers (*.*)|*.*";
Nullable<bool> result = dlg.ShowDialog();
string filename= string.Empty;
if (result == true)
filename = dlg.FileName;
if (!System.IO.Directory.Exists(destinationPath))
System.IO.Directory.CreateDirectory(destinationPath);
byte[] buffer = File.ReadAllBytes(filename);
MemoryStream ms = new MemoryStream(buffer);
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = ms;
bitmap.DecodePixelWidth = 300;
bitmap.EndInit();
bitmap.Freeze();
BitmapEncoder imgEncoder = new JpegBitmapEncoder();
imgEncoder.Frames.Add(BitmapFrame.Create(bitmap));
destinationPath = destinationPath + "\\" + DateTime.Now.ToString() + ".jpg";
FileStream fs = new FileStream(destinationPath, FileMode.Create);
imgEncoder.Save(fs);
newEquipment.pPicturePath = destinationPath;
ms.Close();
fs.Flush();
fs.Close();
//ms.Dispose();
//fs.Dispose();
} |
Partager