2016-08-22 2 views
-1

を保存するとき、私は私のプロジェクトで指定されたパスにビットマップから作成した画像を保存しようとしていますが、私はこのエラーを取得:Additional information: A generic error occurred in GDI+.ASP.NET MVCエラー画像

は、これはコードです:

 public void SaveCaptchaImage(string name) 
     { 
      Bitmap image = new Bitmap(128, 64); 
      string path = "/content/images"; 

      if (System.IO.File.Exists(path + name)) 
       System.IO.File.Delete(path + name); 

      using (Pen myPen = new Pen(Color.Red, 5)) 
      using (Brush myBrush = new SolidBrush(Color.Silver)) 
      using (Font myFont = new Font("Arial", 16)) 
      using (Graphics graphObject = Graphics.FromImage(image)) 
      { 
       graphObject.FillRectangle(myBrush, new Rectangle(0, 0, 128, 64)); 
       graphObject.DrawString(GetRandomCaptcha(), myFont, myBrush, new Point(20, 20)); 
       image.Save(path + name + ".png", ImageFormat.Png); 
      } 
      image.Dispose(); 
     } 

例外は、この行で発生します。

image.Save(path + name + ".png", ImageFormat.Png); 

私はこの問題を解決するために何ができますか?

を編集してください:なぜ私はdownvotedになったのですか。

+0

文字列を連結するのではなく、パスを結合するのに常に 'Path.Combine'を使うことをお勧めします。' name'がスラッシュで始まらないと、 'image.Save(" pathname.png "、ImageFormat.Png);' image.Save( "path/name.png"、ImageFormat.Png)ではなく "ImageFormat.Png" – stuartd

答えて

0

Server.MapPathなどでパスをラップ:

image.Save(HttpContext.Current.Server.MapPath(Path.Combine(path, name + ".png")), ImageFormat.Png); 

ではなく、相対の絶対パスを取得します。

とコメントの中で@stuartdのようにPath.Combineメソッドを使用してください。

関連する問題