2014-11-05 75 views
0

我我的WPF MVVM應用我想上傳到database.The碼圖像操作正常,並且將圖像保存到數據庫作爲image.I需要實施調整而upload.I圖像意思是當點擊上傳圖片將動態調整,並保存到數據庫 這裏是我的代碼調整圖片大小,而文件上傳

public void Upload(object obj) 
{ 
    try 
    { 
     Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); 
     dlg.DefaultExt = ".png"; 
     dlg.Filter = "Image files (*.png;*.jpg)|*.png;*.jpg"; 
     Nullable<bool> result = dlg.ShowDialog(); 
     if (result == true) 
     { 
      string filename = dlg.FileName; 
      UploadText = filename; 
      FileStream FS = new FileStream(filename, FileMode.Open, FileAccess.Read); 
      byte[] img = new byte[FS.Length]; 
      FS.Read(img, 0, Convert.ToInt32(FS.Length)); 
      UploadLogo = img; 
      Stream reader = File.OpenRead(filename); 
      System.Drawing.Image photo = System.Drawing.Image.FromStream((Stream)reader); 
      MemoryStream finalStream = new MemoryStream(); 
      photo.Save(finalStream, ImageFormat.Png); 
      // translate to image source 
      PngBitmapDecoder decoder = new PngBitmapDecoder(finalStream, BitmapCreateOptions.PreservePixelFormat, 
               BitmapCacheOption.Default); 
      ClientLogo = decoder.Frames[0]; ; 
     } 
    } 

    catch (Exception ex) 
    { 
     throw ex; 
    } 
} 

請幫

在此先感謝

+0

[相關(http://stackoverflow.com/q/15779564/1997232) – Sinatr 2014-11-05 14:46:52

回答

0

我完全不爲你的平臺知道編程,但我知道的方法,該方法包括添加了檢查,以測量的UIImage的寬度和高度。如果超過一定的像素是更大的,你可以用UIGraphics的幫助下調整其大小。它應該是這個樣子:

if (image.Size.Width >= 1000 || image.Size.Height >= 1000) 
{ 
    var width = images [i].Size.Width; 
    var height = images [i].Size.Height; 
    var newWidth = 900; 
    var newHeigth = height * newWidth/width; 

    UIGraphics.BeginImageContext (new SizeF (newWidth, newHeigth)); 
    image.Draw (new RectangleF (0, 0, newWidth, newHeigth)); 
    image = UIGraphics.GetImageFromCurrentImageContext(); 
    UIGraphics.EndImageContext(); 
} 

再說一遍,我不知道這個方法適用於你的編程平臺,但你可以試試看。

希望這會有所幫助。祝你好運!