I'm trying to save a file in specified format, inside an mvc controller, by passing the file 'InputStream' to the Bitmap object, and save the bitmap, instead of save the file itself... as i want to process it.
for first step, i tried writing as what as i said, and pas a png, and then a jpg file i found on my pc, but i receive the following error:
A generic error occurred in GDI+.
My code are as following:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,HotelId")] Image image) //, HttpPostedFileBase file)
{
int arrayStartX;
if (ModelState.IsValid)
{
if (Request.Files.Count > 0)
{
var file = Request.Files[0];
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var pathMain = Path.Combine(Server.MapPath("~/Images/HotelGallery/"),
"Img" + image.HotelId + "_" + fileName.Remove(fileName.LastIndexOf('.')) + ".jpg");
//file.SaveAs(path);
try
{
file.InputStream.Seek(0, SeekOrigin.Begin);
Bitmap mainBmp = new Bitmap(file.InputStream);
mainBmp.Save(pathMain, ImageFormat.Jpeg);