Error trying to save Asp . Net MVC image

Asked

Viewed 551 times

0

Good morning People, I am trying to open, resize and save an image again through an ASP application. But I am getting an error when the image will be saved. Follow 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 making in Exception is "GDI+ Generic Error" and in the line "myImg.Save..", but I can’t understand what it is. I would like to thank 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 open this saved image, resize it (85x104) and save the new image in the same or another directory.

Note: The "Capture()" method is working, but I didn’t do it, I was only in charge of creating this new method.

  • 1

    Make sure you have enough permissions to save the files on this location. This is a very common error.

  • 1

    You are hiding the exception and doing nothing useful. All this code could be just this: var myImg = Image.FromFile($@"C:\Desenvolvimento\Sistema\Captures\nome.png");
 myImg = resizeImage(myImg, new Size(85, 104));
 myImg.Save($@"C:\Desenvolvimento\Sistema\Captures\nome.png", ImageFormat.Png);
 It can be simplified to help you see the problem better. And it could give you more information. It looks like you copied someone’s code. Don’t do anything in your code without understanding it. Note that this code you picked up from somewhere is pretty bad.

  • Hello, thanks for the answers, #Caiodepaulasilva I have permission to save files in place, there is another routine in this same program that saves images in this folder. #Maniero I simplified the code as you suggested, still giving the same error in the Exception message. And really I took the code from somewhere else, I didn’t know how to do, I’ll try to understand better.

  • @Q.Wesley, do you have permission... but the user running the service? It’s on IIS Express, on IIS ?

  • Hello @Leandroangelo, I’m sorry, I didn’t quite understand your question. So, in this same program there is already a routine that saves images in a folder, the routine that I posted in the topic takes the image that was already saved and resizes it and saved in another folder (there in the topic this same folder, but I will edit), I understand that the same user runs both routines, isn’t that it? Thank you for your reply.

  • In the code of your question it seems to me that you are opening a file, editing and trying to save it still open... As for another routine, does it work in the same directory? What is the origin of the method resizeImage() ?

  • @Leandroangelo, I made some changes to the topic giving more information about the problem.

Show 2 more comments

1 answer

0

Recently, in a study application I obtained this same error ("GDI+" Generic Error). 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 don’t know if it’s an elegant solution, but it solved the error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.