Search code examples
c#asp.netipadjpeggdi+

A Generic error occurred in GDI+ from picture of Ipad Air


Hi I have this error when a try to upload a picture downloaded via email from an Ipad Air Camera(JPG)

A Generic error occurred in GDI+

Here is the code, anyone can helps me? All the pictures are saving good except from this Ipad Air.

                var pic = System.Web.HttpContext.Current.Request.Files[0];// canvi per Vendor/fileuploadmaster
                Bitmap bmp = new Bitmap(pic.InputStream);
                DateTime dtaken;
                PropertyItem propItem;

                try {

                propItem = bmp.GetPropertyItem(36867);
                string sdate = System.Text.Encoding.UTF8.GetString(propItem.Value).Trim();
                string secondhalf = sdate.Substring(sdate.IndexOf(" "), (sdate.Length - sdate.IndexOf(" ")));
                string firsthalf = sdate.Substring(0, 10);
                firsthalf = firsthalf.Replace(":", "-");
                sdate = firsthalf + secondhalf;
                dtaken = DateTime.Parse(sdate);

                }
                catch {

                dtaken = DateTime.Now;

                }


                //Fecha de última modificacion
                //PropertyItem propItem = bmp.GetPropertyItem(306);


                var newFilePath = ConfigurationManager.AppSettings["PathTmpPhotos"].ToString();

                var FechaString = dtaken.ToString("yyMMddHHmmss");

                var newFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + visitBrandId.ToString("D10") + "_" + FechaString + "_" + pic.FileName;
                var tmpFilePath = newFilePath + newFileName;

                pic.SaveAs(tmpFilePath);

                var img = FileUtilities.ResizePhoto(newFilePath, newFileName, 800, 600);

                ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);

                // Create an Encoder object based on the GUID
                // for the Quality parameter category.
                Encoder myEncoder = Encoder.Quality;

                // Create an EncoderParameters object.
                // An EncoderParameters object has an array of EncoderParameter
                // objects. In this case, there is only one
                // EncoderParameter object in the array.
                EncoderParameters myEncoderParameters = new EncoderParameters(1);

                // 0L = NO Quality // 100L = High Quality
                EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);
                myEncoderParameters.Param[0] = myEncoderParameter;

                img.Save(Path.Combine(newFilePath, newFileName), jpgEncoder, myEncoderParameters);
                //img.Save(Path.Combine(newFilePath, newFileName));

Solution

  • The solution was in the ResizePhoto.

    When the picture was taken with the mobile, is under 800x600 size, so this function cant resize the picture to a bigger size.

    So, the solution was this:

    var img = FileUtilities.ResizePhoto(newFilePath, newFileName, 300, 200);

    Because dont exist any picture taken with the mobile smaller than 300x200

    Regards