2015-08-09 30 views
7

Yüklenen bir görüntüyü MVC 6'da yeniden boyutlandırmanın en iyi yolu nedir? Daha sonra görüntülenecek olanı seçmek için bir görüntünün (örneğin, küçük, büyük, vb.) Birden çok çeşidini saklamak istiyorum.Yüklenen resmi MVC'ye yeniden boyutlandırma 6

İşte eylem için kodum.

[HttpPost] 
    public async Task<IActionResult> UploadPhoto() 
    { 
     if (Request.Form.Files.Count != 1) 
      return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest); 

     IFormFile file = Request.Form.Files[0]; 

     // calculate hash 
     var sha = System.Security.Cryptography.SHA256.Create(); 
     byte[] hash = sha.ComputeHash(file.OpenReadStream()); 

     // calculate name and patch where to store the file 
     string extention = ExtentionFromContentType(file.ContentType); 
     if (String.IsNullOrEmpty(extention)) 
      return HttpBadRequest("File type not supported"); 

     string name = WebEncoders.Base64UrlEncode(hash) + extention; 
     string path = "uploads/photo/" + name; 

     // save the file 
     await file.SaveAsAsync(this.HostingEnvironment.MapPath(path)); 
    } 

cevap

4

Image Processor kütüphanesini kullanmanızı öneririm. Ben MVC içinde ne olduğunu bilmiyorum (

using (var imageFactory = new ImageFactory()) 
using (var fileStream = new FileStream(path)) 
{ 
    file.Value.Seek(0, SeekOrigin.Begin); 

    imageFactory.FixGamma = false; 
    imageFactory.Load(file.Value) 
       .Resize(new ResizeLayer(new Size(264, 176))) 
       .Format(new JpegFormat 
       { 
        Quality = 100 
       }) 
       .Quality(100) 
       .Save(fileStream); 
} 

Nerede file.Value yüklendiği dosya (akış) 'dir:

http://imageprocessor.org/imageprocessor/

Sonra sadece çizgisinde bir şeyler yapabilir Bu, bir Nancy projesinde kullandığım koddur)

+0

Teşekkürler! Tam olarak ihtiyacım olan şey! –

+0

@Phill Harika bağlantı, teşekkürler, vNext görüntüsü için bulduğum tek şey. Ama "Yeni Boyut" System.Drawing kısmı mı? – Alex

+0

@voo geçerli Nuget hala System.Drawing gerektirir, ancak API bu kısıtlamayı bırakmak için V2 için değişiyor. https://github.com/JimBobSquarePants/ImageProcessor#api-changes – Phill

İlgili konular