2014-06-17 32 views
0

大家好我有以下代碼運行在一個WPF窗體後面有2個txt框。 1是圖像的src,另一個是目標路徑。在使用WPF表格的GDI +中發生了一般性錯誤

private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     ProcessLbl.Content = ""; 
     string src = ImgSrc.Text; 
     string dest = ImgDest.Text; 
     string copyrightTxt = CopyWriteTxt.Text; 
     string[] filePaths = Directory.GetFiles(src, "*.jpg"); 
     MemoryStream ms = null; 
     foreach (string f in filePaths) 
     { 
      try 
      { 
       ProcessLbl.Content += src + "\\" + f.ToString() + Environment.NewLine; 
       System.Drawing.Image thumbnail_image = null; 
       System.Drawing.Image original_image = null; 
       System.Drawing.Bitmap final_image = null; 
       System.Drawing.Graphics graphic = null; 
       original_image = System.Drawing.Image.FromFile(System.IO.Path.Combine(src,f)); 

       int width = original_image.Width; 
       int height = original_image.Height; 
       int target_width = 500; 
       int target_height = 367; 
       int new_width, new_height; 

       float target_ratio = (float)target_width/(float)target_height; 
       float image_ratio = (float)width/(float)height; 

       if (target_ratio > image_ratio) 
       { 
        new_height = target_height; 
        new_width = (int)Math.Floor(image_ratio * (float)target_height); 
       } 
       else 
       { 
        new_height = (int)Math.Floor((float)target_width/image_ratio); 
        new_width = target_width; 
       } 

       new_width = new_width > target_width ? target_width : new_width; 
       new_height = new_height > target_height ? target_height : new_height; 
       final_image = new System.Drawing.Bitmap(target_width, target_height); 
       graphic = System.Drawing.Graphics.FromImage(final_image); 
       graphic.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.ColorTranslator.FromHtml("#c2c6c9")), new System.Drawing.Rectangle(0, 0, target_width, target_height)); 
       int paste_x = (target_width - new_width)/2; 
       int paste_y = (target_height - new_height)/2; 

       graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
       graphic.DrawImage(original_image, paste_x, paste_y, new_width, new_height); 
       final_image.Save(System.IO.Path.Combine(dest, f), System.Drawing.Imaging.ImageFormat.Jpeg); 
      } 
      catch (Exception ResizeExp) 
      { 
       System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ResizeExp, true); 
       ProcessLbl.Content += Environment.NewLine; 
       ProcessLbl.Content += Environment.NewLine; 
       ProcessLbl.Content += ResizeExp.Message + " " + trace.GetFrame(0).GetFileLineNumber(); 
       ProcessLbl.Content += Environment.NewLine; 
       ProcessLbl.Content += Environment.NewLine; 
      } 
     } 
     //ProcessLbl 
    } 
} 

我已經在網頁上運行一些類似的代碼,並且工作得很好,但形成某種原因,我得到的錯誤「發生在GDI一般性錯誤+」

誰能幫助?

感謝

回答

0

檢查,如果你有適當的權限來保存位圖中指定的目的地。 嘗試使用其他位置並查看錯誤是否得到解決。如果您需要使用此位置本身,則需要通過清單修改獲取管理權限。

相關問題