Jack
Jack

Reputation: 16724

A Generic error occured in GDI+ in Image.Save() method

I'm trying to use this class but I'm getting a Generic error occured in GDI+ in Image.Save() method. From what I read it's some stream I need to close but I don't know which one.

I'm calling using this:

 Image image = Image.FromFile(@"C:\a.jpg");
            using (var resized = ImageUtilities.ResizeImage(image, 50, 100))
            {
                //save the resized image as a jpeg with a quality of 90
                ImageUtilities.SaveJpeg(@"C:\myimage.jpeg", resized, 90);
            }

Why is that error and how do I solve this?

Upvotes: 0

Views: 344

Answers (2)

James South
James South

Reputation: 10645

Have you tested saving the images in different locations?

If it is still failing then without knowing exactly what is going on in your code I would hazard a guess to say that the original image is getting disposed somewhere before it should be. That's usually the most common cause of the error.

I've written a library that handles many different imaging operations whilst ensuring that memory is correctly handled. It's well tested and very simple to use.

You can get it here. http://imageprocessor.org/

Example code using the library:

using (ImageFactory imageFactory = new ImageFactory())
{
    // Load, resize, set the quality and save an image.
    imageFactory.Load(@"C:\a.jpg")
                .Resize(new Size(50, 100))
                .Quality(90)
                .Save(@"C:\myimage.jpeg);
}

Upvotes: 1

Scott Chamberlain
Scott Chamberlain

Reputation: 127603

Unless your program is running as administrator you can not save directly to the root of C: make a folder and save it inside there instead.

Upvotes: 2

Related Questions