0

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?

Community
  • 1
  • 1
Jack
  • 16,276
  • 55
  • 159
  • 284

2 Answers2

2

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.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • I believe that restriction started with Windows 7. http://answers.microsoft.com/en-us/windows/forum/windows_7-security/windows-7-cannot-save-files-to-c-even-after-making/938f2b50-b063-475b-8c5e-905d136df2e3?page=3 – Steve Wellens Jul 24 '14 at 02:06
  • 2
    @SteveWellens The restriction that only Administrators can write to `C:` has been there since XP, however the fact that people who are members of the administrators group do not run their programs as administrators by default began with Visa and UAC. – Scott Chamberlain Jul 24 '14 at 02:18
1

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);
}
James South
  • 10,147
  • 4
  • 59
  • 115