2010-02-19 54 views
1

在我的應用程序中,我正在將圖像上傳到數據庫中。在存儲在數據庫中之前,我想要進行圖像管理,如減小圖像大小和減小圖像的高度和寬度。你能幫我嗎。有沒有任何源代碼或任何參考請。在asp.net中的圖像管理?

回答

0

如果你在談論的東西像創建一個縮略圖。 Image()類將允許您向上或向下縮放現有圖像。因人而異。

+0

是的確實先生不退款沒有退貨 –

0

您想查看用於使用ASP.NET處理圖像的System.Drawing名稱空間。您可以使用Image.FromFile(),Image.FromStream()等加載任何支持的圖像文件類型(即jpg,gif,png等)。從那裏您可以使用繪圖圖形上下文來操作圖像。這裏給你一個風格是我的調整圖像功能:

// Creates a re-sized image from the SourceFile provided that retails the same aspect ratio of the SourceImage. 
// - If either the width or height dimensions is not provided then the resized image will use the 
//  proportion of the provided dimension to calculate the missing one. 
// - If both the width and height are provided then the resized image will have the dimensions provided 
//  with the sides of the excess portions clipped from the center of the image. 
public static Image ResizeImage(Image sourceImage, int? newWidth, int? newHeight) 
{ 
    bool doNotScale = newWidth == null || newHeight == null; ; 

    if (newWidth == null) 
    { 
     newWidth = (int)(sourceImage.Width * ((float)newHeight/sourceImage.Height)); 
    } 
    else if (newHeight == null) 
    { 
     newHeight = (int)(sourceImage.Height * ((float)newWidth)/sourceImage.Width); 
    } 

    var targetImage = new Bitmap(newWidth.Value, newHeight.Value); 

    Rectangle srcRect; 
    var desRect = new Rectangle(0, 0, newWidth.Value, newHeight.Value); 

    if (doNotScale) 
    { 
     srcRect = new Rectangle(0, 0, sourceImage.Width, sourceImage.Height); 
    } 
    else 
    { 
     if (sourceImage.Height > sourceImage.Width) 
     { 
      // clip the height 
      int delta = sourceImage.Height - sourceImage.Width; 
      srcRect = new Rectangle(0, delta/2, sourceImage.Width, sourceImage.Width); 
     } 
     else 
     { 
      // clip the width 
      int delta = sourceImage.Width - sourceImage.Height; 
      srcRect = new Rectangle(delta/2, 0, sourceImage.Height, sourceImage.Height); 
     } 
    } 

    using (var g = Graphics.FromImage(targetImage)) 
    { 
     g.SmoothingMode = SmoothingMode.HighQuality; 
     g.InterpolationMode = InterpolationMode.HighQualityBicubic; 

     g.DrawImage(sourceImage, desRect, srcRect, GraphicsUnit.Pixel); 
    } 

    return targetImage; 
} 
0

您可以使用圖像類或第三方DLL,如ASPJPEG。幾個ASPJPEG樣本可以在here找到。我做了很多圖像處理,並且我的主機reliablesite在他們的服務器上支持這個dll。