2013-04-24 1 views
0

오후에 asp.net FileUpload 컨트롤을 사용하여 파일을 업로드하려고합니다. 이미지를 저장하기 전에 사용자 GUID로 파일 이름을 바꾼 다음 이미지 크기를 128px로 조정해야합니다.이미지 업로드, 이름 바꾸기 및 크기 조정시 ASP.NET 오류가 발생했습니다.

그러나 저장하려고하면 오류가 발생합니다. GDI +에서 일반 오류가 발생했습니다. 그리고이 정렬 방법을 알아낼 수 없습니다.

사람은 갈등을 덮어 파일로 간단하게 뭔가를 내 코드를 통해보고

protected void btnUpload_Click(object sender, EventArgs e) 
    { 
     Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey; 

     string directory = Server.MapPath("imgs/users"); 
     string fileExt = Path.GetExtension(fuSample.PostedFile.FileName); 
     string fileName = userGuid + fileExt; 

     //Check File ext, make sure its an image! 
     if (fileExt == ".jpeg" || fileExt == ".jpg" || fileExt == ".png" || fileExt == ".gif") 
     { 
      //Now we check to make sure its less than 600kb 
      if (fuSample.PostedFile.ContentLength < 614400) 
      { 
       //Delete an existing file. 
       if (File.Exists(directory + fileName)) 
        File.Delete(directory + fileName); 

       // Create a bitmap of the content of the fileUpload control in memory 
       Bitmap originalBMP = new Bitmap(fuSample.FileContent); 

       // Calculate the new image dimensions 
       int origWidth = originalBMP.Width; 
       int origHeight = originalBMP.Height; 
       int sngRatio = origWidth/origHeight; 
       int newWidth = 128; 
       int newHeight = newWidth/sngRatio; 

       // Create a new bitmap which will hold the previous resized bitmap 
       Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight); 
       // Create a graphic based on the new bitmap 
       Graphics oGraphics = Graphics.FromImage(newBMP); 

       // Set the properties for the new graphic file 
       oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 
       // Draw the new graphic based on the resized bitmap 
       oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight); 

       // Save the new graphic file to the server 
       newBMP.Save(directory + fileName); 

       // Once finished with the bitmap objects, we deallocate them. 
       originalBMP.Dispose(); 
       newBMP.Dispose(); 
       oGraphics.Dispose(); 


       //Success! 

      } 
      else 
      { 
       //error here, img too big 
      } 
     } 
     else 
     { 
      notifybar.Attributes.Add("style", "display:block;"); 
      notifybar.Attributes.Add("class", "failed"); 
      notifyText.Text = "Valid Image files only please! (.png, .jpg, .jpeg, or .gif)"; 
     } 
    } 

답변

0

그것은있을 수하시기 바랍니다 몇 가지 지침을 제공 할 수있다. 그러나이 코드에는 많은 위험한 버그가 있으며 RAM을 매우 효율적으로 사용하지 않습니다.

var j = new ImageJob(fuSample,"~/imgs/users/<guid>.<ext>", 
    new ResizeSettings("maxwidth=128;maxheight=128")); 
j.Build(); 
string imageUrl = PathUtils.GuessVirtualPath(j.FinalPath) 
:

I합니다 (ImageResizer NuGet 패키지를 사용) 다른 접근 방식을 제안

관련 문제