EPPlus icon indicating copy to clipboard operation
EPPlus copied to clipboard

Image file with Incorrect extension

Open raboud opened this issue 3 years ago • 1 comments

I received an excel file from a third party.

The file contains an image with the extension of ".tmp" instead of ".png"

The bytes loaded from the file contain the signature of a png file (0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A)

Most but not all the file types contain signatures. It may be more reliable to rely on the signatures instead of file extension where there is binary data available.

               byte[] iby = ((MemoryStream)Part.GetStream()).ToArray();
               Image = new ExcelImage(this);
               Image.SetImage(iby, PictureStore.GetPictureType(extension));

becomes

                byte[] iby = ((MemoryStream)Part.GetStream()).ToArray();
                Image = new ExcelImage(this);
                if (iby.Length > 2 && iby[0] == 0x42 && iby[1] == 0x4d)
                {
                    ContentType = "image/bmp";
                    Image.SetImage(iby, ePictureType.Bmp);
                }
                else if (iby.Length > 4 && (
                    (iby[0] == 0xff && iby[1] == 0xd8 && iby[2] == 0xff && iby[3] == 0xe0) ||   // jpeg image
                    (iby[0] == 0xff && iby[1] == 0xd8 && iby[2] == 0xff && iby[3] == 0xe2) ||   // Cannon EOS jpeg
                    (iby[0] == 0xff && iby[1] == 0xd8 && iby[2] == 0xff && iby[3] == 0xe3) ||   // Samsung D500 jpeg
                    (iby[0] == 0xff && iby[1] == 0xd8 && iby[2] == 0xff && iby[3] == 0xe8)))    // Still Picture Interchange
                {
                    ContentType = "image/jpeg";
                    Image.SetImage(iby, ePictureType.Jpg);
                }
                else if (iby.Length > 4 && iby[0] == 0x47 && iby[1] == 0x49 && iby[2] == 0x46 && iby[3] == 0x38)
                {
                    ContentType = "image/gif";
                    Image.SetImage(iby, ePictureType.Gif);
                }
                else if (iby.Length > 4 && iby[0] == 0xd7 && iby[1] == 0xcd && iby[2] == 0xc6 && iby[3] == 0x9a)
                {
                    ContentType = "image/x-wmf";
                    Image.SetImage(iby, ePictureType.Wmf);
                }
                else if (iby.Length > 4 && iby[0] == 0x89 && iby[1] == 0x50 && iby[2] == 0x4e && iby[3] == 0x47 &&
                                      iby[4] == 0x0d && iby[5] == 0x0a && iby[6] == 0x1a && iby[7] == 0x0a)
                {
                    ContentType = "image/png";
                    Image.SetImage(iby, ePictureType.Png);
                }
                else
                {
                    Image.SetImage(iby, PictureStore.GetPictureType(extension));
                }

raboud avatar May 19 '22 13:05 raboud

Yes, we have thought about changing that. Actually we already have this functionality build in... https://github.com/EPPlusSoftware/EPPlus/blob/ea60deb2d4989e8da75d2e8b36a4edca28363e9b/src/EPPlus/Drawing/ImageHandling/ImageReader.cs#L34 . But as we use both the EPPlus build in image reader and System.Drawing (for Windows), it requires some extra work. It's a good idea and we will look at it for a future version.

JanKallman avatar May 20 '22 06:05 JanKallman