0

On image upload I want to make copy of that image save with different name and resize dimensions.

[HttpPost]
public ActionResult Create(HttpPostedFileBase photo)
{
    string path =  System.Configuration.ConfigurationManager.AppSettings["propertyPhotoPath"].ToString(); 
    if ((photo != null) && (photo.ContentLength > 0))
    {
        var fileName = Path.GetFileName(photo.FileName);
        var pathToSaveOnHdd = Path.Combine(Server.MapPath(path), fileName);
        string dbPhotoPath = string.Format("{0}{1}", path, fileName);
    }
... 
//        to do: make image copy, change dimensions
}

2 Answers 2

2

To copy a file you could use the File.Copy method. To resize an image, there are many techniques including GDI+, WIC, WPF (here's an example in a similar post) or a NuGet such as ImageResizer.

Sign up to request clarification or add additional context in comments.

Comments

0

You can convert the uploaded file into byte from the ActionController and change the size of stream as shown below

Byte[] image1 = new Byte[photo.ContentLength - 1]; HttpPostedFileBase file = photo.PostedFile; file.InputStream.Read(image1, 0, file.ContentLength - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(image1);

and you can use the graphic class to redraw the image with the desired size as shown below System.Drawing.Image image = Image.FromStream(ms);

Graphic graphic = Graphics.FromImage(image); graphic.DrawImage(image, 0, 0, image.Width, image.Height);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.