0

Only for iPhone 11 images (from what I can tell) I get an error trying to open .jpg files with C#. All other files work perfectly. This is not one phone but multiple phones. I tried the three methods below, all fail. Note: trying to open any of these images in Photoshop fails with a "Program Error"

using (System.Drawing.Image img = System.Drawing.Image.FromFile(physicalPath))  **// out of memory**
{
    if (img != null)
        imgWH = new Dimensions { Width = img.Width, Height = img.Height };
}
                    
Bitmap imgBit = new Bitmap(physicalPath); // **invalid paramater error**
var imageHeight = imgBit.Height;
var imageWidth = imgBit.Width;
imgWH = new Dimensions { Width = imageWidth, Height = imageHeight };

using (FileStream fs = new FileStream(physicalPath, FileMode.Open, FileAccess.Read))
{
    using (System.Drawing.Image original = System.Drawing.Image.FromStream(fs, false, false)) // **invalid paramater**
    {
        if (original != null)
            imgWH = new Dimensions { Width = original.Width, Height = original.Height };
    }
}
Steven B
  • 19
  • 4
  • Can you show us the full error message including possible inner-exception? Have you checked if you can open these files on any other system or in another program other than photoshop? if photoshop fails to load the image that might indicate that maybe the jpg is broken or is not a jpg file at all – D.J. Aug 02 '22 at 19:38
  • System.ArgumentException HResult=0x80070057 Message=Parameter is not valid. This exception was originally thrown at this call stack: System.Drawing.Image.FromStream(System.IO.Stream, bool, bool) I can only open the images in Microsoft Paint. If I save as a .jpg from Paint I can successfully run it through the code. – Steven B Aug 02 '22 at 20:32
  • You phone image driver does not like the image format of the file. Images have a ascii header to if you open the image with notepad (or any text editor) you will see the image type. Then check you phone documentation to see if it accepts the image type. You may need to update (or add) a driver to your phone that accepts the image type. – jdweng Aug 03 '22 at 01:30
  • The ASP.Net code above is running on a Windows server and applied to images uploaded to the server. The images upload successfully but when trying to determine their size with the C# code it throws the error. This happens often enough on some images that I don't believe it to just be an occasional corrupt image. – Steven B Aug 03 '22 at 02:08

0 Answers0