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

worksheet.Drawings.AddPicture error: No image handler for image type Png #721

Closed
elfasito opened this issue Sep 26, 2022 · 11 comments
Closed
Assignees

Comments

@elfasito
Copy link

elfasito commented Sep 26, 2022

Hi,
Im still experiencing this issue on android (on windows works fine)
I tried differents PNG files and I tried changing the code a little to add a jpg, but I get the same but in jpg handler

Im testing with: EPPlus.6.0.8.1056-20220926 version
png example: https://imgbb.com/VLsyKX9
log:

Unity   :   InvalidOperationException: No image handler for image type Png
Unity   :   at OfficeOpenXml.Drawing.PictureStore.GetImageBounds (System.Byte[] image, OfficeOpenXml.Drawing.ePictureType type, OfficeOpenXml.ExcelPackage pck) [0x00000] in <00000000000000000000000000000000>:0
Unity   :   at OfficeOpenXml.Drawing.PictureStore.AddImage (System.Byte[] image, System.Uri uri, System.Nullable`1[T] pictureType) [0x00000] in <00000000000000000000000000000000>:0
Unity   :   at OfficeOpenXml.Drawing.ExcelPicture.SaveImageToPackage (OfficeOpenXml.Drawing.ePictureType type, System.Byte[] img) [0x00000] in <00000000000000000000000000000000>:0
Unity   :   at OfficeOpenXml.Drawing.ExcelPicture.LoadImage (System.IO.Stream stream, OfficeOpenXml.Drawing.ePictureType type) [0x00000] in <00000000000000000000000000000000>:0
Unity   :   at OfficeOpenXml.Drawing.ExcelDrawings.AddPicture (System.String Name, System.IO.FileInfo ImageFile, System.Uri Hyperlink) [0x00000] in <00000000000000000000000000000000>:0
Unity   :   at OfficeOpenXml.Drawing.ExcelDrawings.AddPicture (System.String Name,

this Is the code to add the img to excel file:

            string imgPath = Application.persistentDataPath + "/xlsx/" + "Sign.png";
            filePath = Application.persistentDataPath + "/xlsx/" + ExcelFileName + ".xlsx";

            //create a fileinfo object of an excel file on the disk
            file = new FileInfo(filePath);
        
            //create a new Excel package from the file
            using (ExcelPackage excelPackage = new ExcelPackage(file))
            {
                excelPackage.Settings.ImageSettings.PrimaryImageHandler = new SystemDrawingImageHandler();
                ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["Hoja1"];
                
                //upload image to xls file
                if (File.Exists(imgPath))
                {
                    byte[] imgFile = File.ReadAllBytes(imgPath);
                    //imgFile = File.ReadAllBytes(imgPath);
                    ExcelPicture pic = worksheet.Drawings.AddPicture("Sign", imgPath);
                    pic.SetPosition(85, 8, 4, 13); //format: (1,2,3,4) > 1:row number, 3:column number
                    pic.SetSize(225, 145);
                }
                else
                {
                    Debug.Log("img to add does not exist");
                }
               excelPackage.Save();
@JanKallman
Copy link
Contributor

Hello,
I get a 403 - Access denied - on the link to the image. Can you supply a new link?

@JanKallman JanKallman self-assigned this Sep 27, 2022
@elfasito
Copy link
Author

Hello, I get a 403 - Access denied - on the link to the image. Can you supply a new link?

My bad, Updated link:
https://imgbb.com/VLsyKX9

@JanKallman
Copy link
Contributor

The png fix was for the build-in GenericImageHandler . You are using the SystemDrawingImageHandler here.
I suggest you try the GenericImageHandler, but if you want to use the SystemDrawingImageHandler make sure you have libgdiplus installed. If you need to install it see this link for samples: https://github.com/EPPlusSoftware/EPPlus/wiki/EPPlus-5-and-Docker

@elfasito
Copy link
Author

elfasito commented Sep 27, 2022

The png fix was for the build-in GenericImageHandler . You are using the SystemDrawingImageHandler here. I suggest you try the GenericImageHandler, but if you want to use the SystemDrawingImageHandler make sure you have libgdiplus installed. If you need to install it see this link for samples: https://github.com/EPPlusSoftware/EPPlus/wiki/EPPlus-5-and-Docker

Hi @JanKallman , how I can use the GenericImageHandler?
I changed the linecode to this:
excelPackage.Settings.ImageSettings.PrimaryImageHandler = new GenericImageHandler();

but im getting the same error.
I need to do something else?.
also, libgdiplus works on android? (if I want to use the new imageHandler)

@JanKallman
Copy link
Contributor

You can just remove that line. GenericImageHandler is the default on non-windows systems.
Libgdiplus is only required if you use the SystemDrawingImageHandler. I have not tried installing libgdiplus on Android.
If you still get this error, can you provide a small sample what you are doing?

@elfasito
Copy link
Author

elfasito commented Sep 28, 2022

Hi @JanKallman I write this reduced sample:

using OfficeOpenXml;
using OfficeOpenXml.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class TestWrite : MonoBehaviour
{
    //SOME EPPLUS VARIABLES
    public string ExcelFileName;
    private string filePath;
    FileInfo file;

    private void Start()
    {
       
        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) //if its android or iOS
        {
            string XLSXrealPath;
            string PNGrealPath;
            //Save the formated excel file into a writeable directory on android
            if (System.IO.File.Exists(Application.persistentDataPath + "/xlsx/" + ExcelFileName + ".xlsx"))
            {
                Debug.Log("Excel File Already exist in app data");
            }
            else
            {
                Directory.CreateDirectory(Application.persistentDataPath + "/xlsx"); //create a directory/folder

                string oriPath = Path.Combine("jar:file:https://" + Application.dataPath + "!/assets/xlsx/", ExcelFileName + ".xlsx"); //returns a DirectoryInfo object
                UnityWebRequest reader = UnityWebRequest.Get(oriPath);
                reader.SendWebRequest();
                while (!reader.isDone) { }
                XLSXrealPath = Application.persistentDataPath + "/xlsx/" + ExcelFileName + ".xlsx"; //save the file with this name
                File.WriteAllBytes(XLSXrealPath, reader.downloadHandler.data);
                Debug.Log("Excel File generated in app data from StreamingAssets");
            }

            //Copy img to a writeable directory on android
            if (System.IO.File.Exists(Application.persistentDataPath + "/xlsx/" + "Sign.png"))
            {
                Debug.Log("PNG File Already exist in app data");
            }
            else
            {                
                string oriPath = Path.Combine("jar:file:https://" + Application.dataPath + "!/assets/xlsx/", "Sign.png"); //returns a DirectoryInfo object
                UnityWebRequest reader = UnityWebRequest.Get(oriPath);
                reader.SendWebRequest();
                while (!reader.isDone) { }
                PNGrealPath = Application.persistentDataPath + "/xlsx/" + "Sign.png"; //save the file with this name
                File.WriteAllBytes(PNGrealPath, reader.downloadHandler.data);
                Debug.Log("PNG File copied in app data from StreamingAssets");
            }
        }
        //Debug.Log(Application.persistentDataPath); //shows files in this directory
    }

    public void TaskWriteXLSX()
    {
        ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
        ExcelPackage.Configure(x => x.SuppressInitializationExceptions = true);
        string imgPath = Application.streamingAssetsPath + "/xlsx/" + "Sign.png";

#if UNITY_EDITOR //only runs on unity editor
        filePath = Application.streamingAssetsPath + "/xlsx/" + ExcelFileName + ".xlsx";

        //create a fileinfo object of an excel file on the disk
        file = new FileInfo(filePath);
        
        using (ExcelPackage excelPackage = new ExcelPackage(file))
        {
            //create an instance of the the first sheet in the loaded file
            ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["Hoja1"];

            //just some cell writing for testing
            worksheet.Cells["B3"].Value = "added some text from epplus";
                        
            //try to set picture at specific position
            if (File.Exists(imgPath))
            {
                byte[] imgFile = File.ReadAllBytes(imgPath);
                imgFile = File.ReadAllBytes(imgPath);               
                ExcelPicture pic = worksheet.Drawings.AddPicture("Sign", imgPath);
                pic.SetPosition(7, 8, 2, 13); //format: (1,2,3,4) > 1:row number, 3:column number
                pic.SetSize(225, 145);
            }
            else
            {
                Debug.Log("img to add does not exist");
            }

            //save the changes
            //excelPackage.Save(); //overwrite the templated excel file, alternative to the below code
            var newFile = new FileInfo(Application.streamingAssetsPath + "/xlsx/" + "testWDATA" + ".xlsx"); //save the templated excel file with a new name and complete some data
            excelPackage.SaveAs(newFile);
            Debug.Log("xlsx file updated");
        }
#endif

        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) //if its android or iOS
        {           
            imgPath = Application.persistentDataPath + "/xlsx/" + "Sign.png";
            //the path of the file
            filePath = Application.persistentDataPath + "/xlsx/" + ExcelFileName + ".xlsx"; 

            //create a fileinfo object of an excel file on the disk
            file = new FileInfo(filePath);

            //create a new Excel package from the file
            using (ExcelPackage excelPackage = new ExcelPackage(file))
            {
                //switch between image Handler, the default is: GenericImageHandler
                //excelPackage.Settings.ImageSettings.PrimaryImageHandler = new SystemDrawingImageHandler();
                //excelPackage.Settings.ImageSettings.PrimaryImageHandler = new GenericImageHandler();

                //create an instance of the the first sheet in the loaded file
                ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["Hoja1"];

                //upload image to xls file
                if (File.Exists(imgPath))
                {
                    //byte[] imgFile = File.ReadAllBytes(imgPath);
                    //imgFile = File.ReadAllBytes(imgPath);
                    ExcelPicture pic = worksheet.Drawings.AddPicture("Sign", imgPath);
                    pic.SetPosition(85, 8, 4, 13); //format: (1,2,3,4) > 1:row number, 3:column number
                    pic.SetSize(225, 145);
                }
                else
                {
                    Debug.Log("img to add does not exist");
                }

                worksheet.Cells["B3"].Value = "added some text from epplus";

                //save the changes
                excelPackage.Save();
                Debug.Log("xlsx file updated");
            }
        }
    }
}

the code works ok on windows, but the section for android shows the error of always.

@JanKallman
Copy link
Contributor

Sorry for the delayed answer. Do you think you can provide a zipped sample project to reproduce this. I can't run this code-snippet without the correct references.

@elfasito
Copy link
Author

elfasito commented Oct 4, 2022

Sorry for the delayed answer. Do you think you can provide a zipped sample project to reproduce this. I can't run this code-snippet without the correct references.

Hi @JanKallman, no problem.
you refer to the unity project?.
here is: https://www12.zippyshare.com/v/qTW2gOxE/file.html

@JanKallman
Copy link
Contributor

JanKallman commented Oct 17, 2022

Hello, I had a look at this, but I'm not that into Unity, so I could not get the project working. I tried it in another similar Linux environment and it seems to work. As Unity seems to use .NET framework, this might be the issue, so try the fix in the latest develop.

@elfasito
Copy link
Author

Hello, I had a look at this, but I'm not that into Unity, so I could not get the project working. I tried it in another similar Linux environment and it seems to work. As Unity seems to use .NET framework, this might be the issue, so try the fix in the latest develop.

Hi @JanKallman , sorry for the late answer, I tested it today with the dev: 6.0.8.1061-20221020-develop
and now its working on android too, thanks.
I need to do some more test for a real case and test it on iOS too, but i guess it will work fine

@JanKallman
Copy link
Contributor

Fixed in EPPlus 6.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants