2016-08-22 4 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 메서드를 사용하는 것이 좋습니다.