2012-08-13 83 views
1

可能重复:
How to generate square thumbnail of an image?如何将图像保存为缩略图MVC3

我想我的图像保存为缩略图。我怎样才能做到这一点 ?

这里是我的动作控制:

[HttpPost] 
[ValidateInput(false)] 
public ActionResult banner_create(banner banner, HttpPostedFileBase file) 
{ 
    var fileName = Path.GetFileName(file.FileName); 
    var path = Path.Combine(Server.MapPath("~/banner_image/"), fileName); 
    var extension = Path.GetExtension(path); 
    file.SaveAs(path); 
    banner.banner_image_description = extension; 
    banner.banner_image_name = fileName; 
    if (ModelState.IsValid) 
    { 
     db.banner.AddObject(banner); 
     db.SaveChanges(); 
     return RedirectToAction("index"); 
    } 

    return View(banner); 
} 

回答

2

下面的代码应该工作正常。 我已经添加了一些评论,所以你可以看到发生了什么。

// First, we convert an HttpPostedFileBase to an Image 
// (Please note that you need to reference System.Drawing.dll) 
using (var image = Image.FromStream(httpPostedFileBase.InputStream, true, true)) 
{ 
    // Then we create a thumbnail. 
    // The simplest way is using Image.GetThumbnailImage: 
    using (var thumb = image.GetThumbnailImage(
     thumbWidth, 
     thumbHeight, 
     () => false, 
     IntPtr.Zero)) 
    { 
     // Finally, we encode and save the thumbnail. 
     var jpgInfo = ImageCodecInfo.GetImageEncoders() 
      .Where(codecInfo => codecInfo.MimeType == "image/jpeg").First(); 

     using (var encParams = new EncoderParameters(1)) 
     { 
      // Your output path 
      string outputPath = "..."; 
      // Image quality (should be in the range [0..100]) 
      long quality = 90; 
      encParams.Param[0] = new EncoderParameter(Encoder.Quality, quality); 
      thumb.Save(outputPath, jpgInfo, encParams); 
     } 
    } 
} 
0

这里是C#函数,您可以使用它来调整图像的大小任何你想要的方式。在你的特定情况下,使其成为一定大小的缩略图。 它需要System.Drawing.Imageint size希望其宽度为并返回System.Drawing.Image。 现在,这个工作是肯定的,我在当前的项目中使用它,它很好地完成了这项工作。

public System.Drawing.Image ScaleBySize(System.Drawing.Image imgPhoto, int size) 
{ 
    var logoSize = size; 

    float sourceWidth = imgPhoto.Width; 
    float sourceHeight = imgPhoto.Height; 
    float destHeight; 
    float destWidth; 
    const int sourceX = 0; 
    const int sourceY = 0; 
    const int destX = 0; 
    const int destY = 0; 

    // Resize Image to have the height = logoSize/2 or width = logoSize. 
    // Height is greater than width, set Height = logoSize and resize width accordingly 
    if (sourceWidth > (2 * sourceHeight)) 
    { 
    destWidth = logoSize; 
    destHeight = sourceHeight * logoSize/sourceWidth; 
    } 
    else 
    { 
    int h = logoSize/2; 
    destHeight = h; 
    destWidth = sourceWidth * h/sourceHeight; 
    } 
    // Width is greater than height, set Width = logoSize and resize height accordingly 

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

     using (Graphics grPhoto = Graphics.FromImage(bmPhoto)) 
     { 
      grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; 
      grPhoto.DrawImage(imgPhoto, 
        new Rectangle(destX, destY, (int)destWidth, (int)destHeight), 
        new Rectangle(sourceX, sourceY, (int)sourceWidth, (int)sourceHeight), 
        GraphicsUnit.Pixel); 
      grPhoto.Dispose(); 
     } 
    return bmPhoto; 
} 

希望这会帮助你。