2011-08-25 63 views
0

我想使用後面的代碼重新調整asp:圖像的大小。我將根據其高度重新調整圖像的大小,以便它可以放置到位。到目前爲止,我有使用代碼隱藏動態更改圖像高度

 Image img1 = ((Image)e.Item.FindControl("prodImage")); 
     int finalWidth = Convert.ToInt32(img1.Width); 
     int maxWidth = 20; 

     if (finalWidth > maxWidth) 
     { 
      resize 
     } 
     else 
     { 
      dont resize 
     } 

我得到一個轉換錯誤,因爲img1.width是一個Web單元。我嘗試過,但無法投射爲int或將整數作爲單位投射。

由於任何人誰可以幫助

+1

你必須像這樣'int finalWidth =(int)img1.Width.Value;' –

+1

請注意,使用上面的代碼只會改變瀏覽器如何縮放圖像,而不會改變實際圖像 –

回答

1

請參閱從 「High Quality Image Scaling C#」 這個答案。

下面是你需要的代碼的相關位:

/// <summary> 
    /// Resize the image to the specified width and height. 
    /// </summary> 
    /// <param name="image">The image to resize.</param> 
    /// <param name="width">The width to resize to.</param> 
    /// <param name="height">The height to resize to.</param> 
    /// <returns>The resized image.</returns> 
    public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height) 
    { 
     //a holder for the result 
     Bitmap result = new Bitmap(width, height); 

     //use a graphics object to draw the resized image into the bitmap 
     using (Graphics graphics = Graphics.FromImage(result)) 
     { 
      //set the resize quality modes to high quality 
      graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
      graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
      graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
      //draw the image into the target bitmap 
      graphics.DrawImage(image, 0, 0, result.Width, result.Height); 
     } 

     //return the resulting bitmap 
     return result; 
    } 
+0

這看起來像它會工作。虐待測試它以後回到雅。謝謝 –

+0

我發現如何在不使用圖形庫的情況下做到這一點 –

1

您可以使用使用此代碼

double ratio = maxWidth/finalWidth; 
int maxHeight = (int) img1.Height * ratio; 
System.Drawing.Image resized= img1.GetThumbnailImage(maxWidth , maxHeight , null, IntPtr.Zero); 

看看這個function

+0

注意:如果您只想要一個小縮略圖,這很棒,但如果圖像質量極差你正在創造更大的東西。優點是性能高。 –

+0

以及20像素看起來對我來說很小。 – Mehran

+0

同意,我只是爲任何發生在這個線程中的人做筆記。 –

0

我發現瞭如何做到這一點,而無需使用圖形庫

 //Create a new image and set it to the image that was loaded in initially. 
     System.Web.UI.WebControls.Image image = loaded image; 
     //Check if the image has a picture and if not put in the no_picture image. 
     if (product.Image1.ToString() == "") 
     { 
      ((System.Web.UI.WebControls.Image)e.Item.FindControl("prodImage")).ImageUrl    = "no_picture.gif"; 
     } 
     else 
     { 
      ((System.Web.UI.WebControls.Image)e.Item.FindControl("prodImage")).ImageUrl = "upload" + product.Image1.ToString();     
     } 
     //Call a function to keep the images within the borders of each repeater cell. Returns an Image object so we can access the new height/width. 
     System.Web.UI.WebControls.Image dummyImg = keepAspectRatio(image, 110, 100); 

     image.Width = new Unit(dummyImg.Width.Value); 
     image.Height = new Unit(dummyImg.Height.Value); 

     public System.Web.UI.WebControls.Image keepAspectRatio(System.Web.UI.WebControls.Image image, double maxWidth, double maxHeight) 
     { 
      //Create an Image object to store width and height 
      System.Web.UI.WebControls.Image dummyImage2 = new System.Web.UI.WebControls.Image();   
      //Ratio variables to calculate aspect 
      double MaxRatio = maxWidth/maxHeight; 
      double ImgRatio = image.Width.Value/image.Height.Value; 
      //Compare the images width that was passed in to the max width allowed 
      if (image.Width.Value > maxWidth) 
      { 
       //Set the dummy images height and width 
       dummyImage2.Height = (int) Math.Round(maxWidth/ImgRatio, 0); 
       dummyImage2.Width = (int)maxWidth;    
      } 
      //Compare the images height that was passed in to the max height allowed 
      if (image.Height.Value > maxHeight) 
      { 
       //Set the dummy images height and width 
       dummyImage2.Height = (int)Math.Round(maxWidth * ImgRatio, 0); 
       dummyImage2.Width = (int)maxHeight;    
      } 
      //Returnthe dummy object so its parameters can be accessed 
      return dummyImage2; 
     } 
0

您可以使用此功能,以保持比例:

   System.Drawing.Size size = LockAspectRatio(sFullFilePathAndName, Convert.ToInt32(rdPhoto.Width.Value) - 20, Convert.ToInt32(rdPhoto.Height.Value) - 80); 
       imgRep.Width = new Unit(size.Width); 
       imgRep.Height = new Unit(size.Height); 


public System.Drawing.Size LockAspectRatio(string sFilePath, double maxWidth, double maxHeight) 
    { 
     int newWidth, newHeight; 
     System.Drawing.Size size = new System.Drawing.Size(); 
     System.Drawing.Image image = System.Drawing.Image.FromFile(sFilePath); 

     size.Width = image.Width; 
     size.Height = image.Height; 

     newWidth = image.Width; 
     newHeight = image.Height; 

     double imgRatio = (double)image.Width/(double)image.Height; 

     //Step 1: If image is bigger than container, shrink it 
     if (image.Width > maxWidth) 
     { 
      size.Height = (int)Math.Round(maxWidth * imgRatio, 0); 
      size.Width = (int)maxWidth; 

      newWidth = size.Width; 
      newHeight = size.Height; 
     } 

     if (newHeight > maxHeight) 
     { 
      size.Height = (int)maxHeight; 
      size.Width = (int)Math.Round(maxHeight * imgRatio, 0); 
     } 

     //Step 2: If image is smaller than container, stretch it (optional) 
     if ((image.Width < maxWidth) && (image.Height < maxHeight)) 
     { 
      if (image.Width < maxWidth) 
      { 
       size.Height = (int)Math.Round(maxWidth * imgRatio, 0); 
       size.Width = (int)maxWidth; 

       newWidth = size.Width; 
       newHeight = size.Height; 
      } 

      if (newHeight > maxHeight) 
      { 
       size.Height = (int)maxHeight; 
       size.Width = (int)Math.Round(maxHeight * imgRatio, 0); 
      } 
     } 
     return size; 
    }