2014-04-18 6 views
0

800 x 600 픽셀보다 큰 이미지는 크기를 조정해야합니다. 가로 세로 비율에 따라 800 X 600 해상도로 크기를 조정해야합니다.이미지의 크기가 픽셀 단위로 손상됩니다.

나는 그것을 크기 다시 코드 아래 사용하고 있습니다 : 그것은 완벽하게 작동하지만 크기 조정 이미지가 픽셀에 손상된

public string ResizeUserImage(string fullFileName, int maxHeight, int maxWidth, string newFileName) 
{ 
     string savepath = System.Web.HttpContext.Current.Server.MapPath("//InitiativeImage//"); 

     try 
     { 
      using (Image originalImage = Image.FromFile(fullFileName)) 
      { 
       int height = originalImage.Height; 
       int width = originalImage.Width; 
       int newHeight = maxHeight; 
       int newWidth = maxWidth; 

       if (height > maxHeight || width > maxWidth) 
       { 
        if (height > maxHeight) 
        { 
         newHeight = maxHeight; 
         float temp = ((float)width/(float)height) * (float)maxHeight; 
         newWidth = Convert.ToInt32(temp); 

         height = newHeight; 
         width = newWidth; 
        } 

        if (width > maxWidth) 
        { 
         newWidth = maxWidth; 
         float temp = ((float)height/(float)width) * (float)maxWidth; 
         newHeight = Convert.ToInt32(temp); 
        } 

        Image.GetThumbnailImageAbort abort = new Image.GetThumbnailImageAbort(ThumbnailCallback); 
        using (Image resizedImage = originalImage.GetThumbnailImage(newWidth, newHeight, abort, System.IntPtr.Zero)) 
        { 
         //When image is compress then store the image 

         var guid = Guid.NewGuid().ToString(); 
         string latestFileName = guid + Path.GetExtension(newFileName); 

         resizedImage.Save(savepath + @"\" + latestFileName); 

         string finalPath = AppSettings.Domain + "InitiativeImage/" + latestFileName; 

         return finalPath; 

        } 


       } 
       else if (fullFileName != newFileName) 
       { 

        //var guid = Guid.NewGuid().ToString(); 
        //newFileName = guid + Path.GetExtension(newFileName); 

        //// no resizing necessary, but need to create new file 
        //originalImage.Save(savepath + @"\" + newFileName); 

        //string finalPath = AppSettings.Domain + "UserImage/" + newFileName; 

        return newFileName; 
       } 


       return fullFileName; 
      } 

     } 
     catch (Exception) 
     { 

      throw; 
     } 
    } 

.

내가 서버에 업로드하고 원본 이미지 아래를 참조를 다시 정립 후

Original image

, 그것은 손해 이미지 아래처럼 :

re-sized image

당신이있어 희망 내 질문. 크기가 조정 된 이미지는 픽셀 단위로 손상됩니다. 제가 잘못되었거나 문제가있는 곳을 알려주십시오. Image.GetThumbnailImage 방법에 대한 설명서에서

+0

는 http://www.hanselman.com/blog/NuGetPackageOfWeek11ImageResizerEnablesCleanClearImageResizingInASPNET.aspx –

답변

2

Pls 아래 기능을 사용해보십시오.

public void FixedSize(string oldImageFile, int Width, int Height,string finalpath) 
    { 
     Bitmap imgPhoto = new Bitmap(oldImageFile); 
     int sourceWidth = imgPhoto.Width; 
     int sourceHeight = imgPhoto.Height; 
     int sourceX = 0; 
     int sourceY = 0; 
     int destX = 0; 
     int destY = 0; 

     float nPercent = 0; 
     float nPercentW = 0; 
     float nPercentH = 0; 

     nPercentW = ((float)Width/(float)sourceWidth); 
     nPercentH = ((float)Height/(float)sourceHeight); 
     if (nPercentH < nPercentW) 
     { 
      nPercent = nPercentH; 
      destX = System.Convert.ToInt16((Width - 
          (sourceWidth * nPercent))/2); 
     } 
     else 
     { 
      nPercent = nPercentW; 
      destY = System.Convert.ToInt16((Height - 
          (sourceHeight * nPercent))/2); 
     } 

     int destWidth = (int)(sourceWidth * nPercent); 
     int destHeight = (int)(sourceHeight * nPercent); 

     Bitmap bmPhoto = new Bitmap(destWidth, destHeight, 
          PixelFormat.Format24bppRgb); 
     bmPhoto.SetResolution(imgPhoto.HorizontalResolution, 
         imgPhoto.VerticalResolution); 

     Graphics grPhoto = Graphics.FromImage(bmPhoto); 
     grPhoto.Clear(Color.White); 
     grPhoto.InterpolationMode = 
       InterpolationMode.HighQualityBicubic; 

     grPhoto.DrawImage(imgPhoto, 
      new Rectangle(0, 0, destWidth, destHeight), 
      new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), 
      GraphicsUnit.Pixel); 

     grPhoto.Dispose(); 
     bmPhoto.Save(finalpath); 
    } 
+1

감사합니다. 이 솔루션은 저에게 효과적입니다. –

1

: 요청 된 섬네일 화상이 120 X 120 픽셀의 크기를 갖는 경우는

GetThumbnailImage 방법이 잘 작동한다. 썸네일이 포함 된 이미지에서 큰 썸네일 이미지 (예 : 300 x 300)를 요청하면 썸네일 이미지의 품질이 눈에 띄게 저하 될 수 있습니다. DrawImage 메서드를 호출하여 기본 이미지의 크기를 조정하는 것이 좋습니다 (포함 된 축소판의 크기를 조정하는 대신).

그러나 객관적인 크기는 약 800x600 픽셀이며 들여 쓰기 용도보다 위에 있습니다.

따라서 이미지 크기 조정에 대해 this answer을 살펴 보시기 바랍니다.