0

My website have a part for image upload from client side.I have problem in showing of images.Because size of pictures are bollix.How can i resize the images when they are uploading?

This my 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();
    }
Joe walter
  • 29
  • 7
  • 4
    Possible duplicate of [Resize an Image C#](http://stackoverflow.com/questions/1922040/resize-an-image-c-sharp) – Paul Abbott Jul 22 '16 at 20:54

1 Answers1

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();  
Motion
  • 116
  • 1
  • 11