3

The following code works fine for almost all images but the one I attached raises this well know error. The only difference this jpg has to other ones I have found with XnView, it says the format is "JPEG TrueColor (v1.1)" were other jpg files have "JPEG TrueColor (v1.2)".

Is this specific to that jpg version?

using (var originalFileStream = new FileStream(picturePath, FileMode.Open, FileAccess.ReadWrite))
{
    var originalImage = Image.FromStream(originalFileStream);

    // ... some image resize ...

    var newFile = @"C:\Temp\temp.jpg";

    // we must keep the original stream open until saving http://stackoverflow.com/a/336396/1328536
    originalImage.Save(newFile);
}

edit 1: StackTrace:

bei System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
bei System.Drawing.Image.Save(String filename, ImageFormat format)
bei System.Drawing.Image.Save(String filename)
bei Namespace.ViewModel.SavePicture(String picturePath) in d:\...\ViewModel.cs:Zeile 119.
bei Namespace.ViewModel.Edit() in d:\...\ViewModel.cs:Zeile 517.

edit 2: remove imgur link since the file was altered on upload

link to zip file containing the jpg

fuchs777
  • 993
  • 1
  • 14
  • 30
  • Works fine for me with the downloaded image. But I don't know whether the downloaded file is identical to your file (might have been altered by imgur). – Dirk Vollmar Dec 22 '15 at 08:41
  • Can you post the full stacktrace? – Dirk Vollmar Dec 22 '15 at 08:42
  • added stacktrace... downloaded the file and that works... seems it is altered by imgur... – fuchs777 Dec 22 '15 at 08:53
  • uploaded the file in a zip to my google drive – fuchs777 Dec 22 '15 at 08:59
  • Also works with the image from the zip file. Are you sure that you can save to the specified location? Have you tried another folder / filename? The problem could of course also be related to the manipulations / resizing that you didn't include above. – Dirk Vollmar Dec 22 '15 at 09:15
  • Checked the same code you see above in an empty WinForms application. The result is the same error. Using Visual Studio Professional 2013 Update 4 on Windows 7 SP1. – fuchs777 Dec 22 '15 at 13:47
  • As I said in my previous comment, the problem might not be in your code but your environment. Have you checked that the path you're going to save to exists and is writable for your process? E.g. if the folder `C:\temp` does not exist you would get an `ExternalException` with a generic error message. – Dirk Vollmar Dec 22 '15 at 14:17
  • Tried it on two other machines and it works on Windows 8.1 but fails on Windows 7... – fuchs777 Dec 22 '15 at 14:24
  • It looks like a problem with your library. I tried several decoders and all had no problem reading the image. "JPEG TrueColor (v1.1)" has nothing to do with JPEG itself, that is something your encoder is adding. – user3344003 Dec 23 '15 at 01:54

0 Answers0