2016-07-22 23 views
0

Meine Website haben einen Teil für Bild-Upload von Client-Seite. Ich habe Probleme mit der Darstellung von Bildern. Weil Größe der Bilder sind bollix.Wie kann ich die Größe der Bilder ändern, wenn sie hochladen?Wie wird die Bildgröße in MVC.NET in der Methode "Binärdatei hochladen" geändert?

Das ist mein Code:

[HttpPost] 
    public ActionResult Create(int id,HttpPostedFileBase photoFile) 
    { 
     if (photoFile != null) 
     { 
      Photo photo = new Photo(); 
      photo.Part = db.Parts.Find(id); 
      photo.PhotoContent = photoFile.ContentType; 
      photo.PhotoByte = new byte[photoFile.ContentLength]; 
      photoFile.InputStream.Read(photo.PhotoByte, 0, photoFile.ContentLength); 
      db.Photos.Add(photo); 
      db.SaveChanges(); 
      return RedirectToAction("Index", new { id = photo.Part.Id }); 
     } 
     return View(); 
    } 
+4

Mögliche Duplikat [Resize eines Bildes C#] (http://stackoverflow.com/questions/1922040/resize-an-image-c-sharp) –

Antwort

0
 Stream str = new MemoryStream((Byte[])photo);   
     Bitmap loBMP = new Bitmap(str); 
     Bitmap bmpOut = new Bitmap(100, 100); 
     Graphics g = Graphics.FromImage(bmpOut); 
     g.InterpolationMode =System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
     g.FillRectangle(Brushes.White, 0, 0, 100, 100); 
     g.DrawImage(loBMP, 0, 0, 100, 100); 
     MemoryStream ms = new MemoryStream(); 
     bmpOut.Save(ms, System.Drawing.Imaging.ImageFormat.Png); 
     byte[] bmpBytes = ms.GetBuffer(); 
     bmpOut.Dispose(); 
     ms.Close(); 
     Response.BinaryWrite(bmpBytes);   
     Response.End();