Error trying to save image Asp .Net MVC

0

Good morning, I'm trying to open, resize, and save an image again through an ASP.NET application, but I'm getting an error when the image will be saved. Here is the code that is giving error:

public void MudarResolução(string nome)
    {
        string fileName = nome;

        string imagem = string.Format("C:\Desenvolvimento\Sistema\Captures\{0}.png", fileName);

        try
        {
            Image myImg = Image.FromFile(imagem);

            myImg = resizeImage(myImg, new Size(85, 104));

            myImg.Save(string.Format("C:\Desenvolvimento\Sistema\Captures\{0}.png", fileName), ImageFormat.Png);



        }
        catch(Exception e)
        {
            throw new Exception();
        }

    }

The error you are giving in Exception is "Generic Error GDI +" and in the "myImg.Save ..." line, but I can not understand what it is. Thanks in advance to anyone who can help.

Another method that is working:

 public ActionResult Capture()
    {
        if (Request.InputStream.Length > 0)
        {
            using (StreamReader reader = new StreamReader(Request.InputStream))
            {
                string hexString = Server.UrlEncode(reader.ReadToEnd());
                string imageName = Convert.ToString(Session["Nome_Foto"]);
                string imagePath = string.Format("~/Captures/{0}.png", imageName);

                System.IO.File.WriteAllBytes(Server.MapPath(imagePath), ConvertHexToBytes(hexString));

                //apenas mostra a imagem capturada na tela a direita
                Session["CapturedImage"] = VirtualPathUtility.ToAbsolute(imagePath);

            }
        }

        MudarResolução(imageName);

        return View();
    }

public static Image resizeImage(Image imgToResize, Size size)
    {
        return (Image)(new Bitmap(imgToResize, size));
    }

Note: The "Capture ()" method saves an image in size 320x240 in the "Captures" directory, what I want to do is to open this saved image, resize it (85x104) and save the new image in the same or directory.

    
asked by anonymous 06.11.2018 / 12:33

1 answer

0

Recently, in a study application I got this same error ("Generic Error GDI +"). The solution was to create a new "temporary" memory buffer to save the created image. In my case I was working with Bitmap format.

    //Create Bitmap Image
    var result = //Imagem em formato bitmap


    string outputfilename = string.Format("C:\Desenvolvimento\Sistema\Captures\{0}.jpg", fileName);

    //SAVE FILE   
    using (MemoryStream memory = new MemoryStream())
    {
       using (FileStream fs = new FileStream(outputfilename, FileMode.Create, FileAccess.ReadWrite))
        {
          result.Save(memory, ImageFormat.Jpeg);
            byte[] bytes = memory.ToArray();
            fs.Write(bytes, 0, bytes.Length);
        }
    }

I do not know if it's an elegant solution, but solved the error.

    
06.11.2018 / 23:11