Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image file with Incorrect extension #646

Open
raboud opened this issue May 19, 2022 · 1 comment
Open

Image file with Incorrect extension #646

raboud opened this issue May 19, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@raboud
Copy link
Contributor

raboud commented May 19, 2022

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));
                }
@JanKallman
Copy link
Contributor

Yes, we have thought about changing that. Actually we already have this functionality build in...

internal ePictureType? GetPictureType(MemoryStream ms)
.
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 JanKallman added the enhancement New feature or request label May 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants