Aspose.Cells Html to Excel conversion not showing images

Hi Amjad Sahi,
We are able to stream the image fine with in our we application.
When we put this url in our webpage the images are loading properly.
I am not sure if this could be the issue.

Hi Amjad Sahi,
I have attached the we application code where we have the image url in out Home/index page and we are able to see the image fine when we launch the app.
so that makes us believe we don’t block the stream.WebApplication7.zip (837.7 KB)

@kumar.penigalapati,
Thank you for sharing sample application. We will analyze it and share our feedback accordingly.

@kumar.penigalapati,

In your web application environment, can you get the image stream from https url using the following code?
e.g
Sample code:

WebRequest request = WebRequest.Create(href);
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();

if your web application is ok, you can get image stream. Can you try it?

Hi Amjad Sahi,
we have tried the example you have given, we were getting an error, i have attached the screen shot.
for some of those outside urls we have to use our proxy server to read them.
i have also attached a code which uses the proxy server if i use it then i am able to read the stream fine.
can you please let us know how we can use the proxy server via Aspose.Cells library so we can get the images stream ?Images-Stream.zip (258.5 KB)

Thank you.

@kumar.penigalapati,

Thanks for the screenshots.

We will evaluate your issue further and get back to you soon.

@kumar.penigalapati,
You can implement the IStreamProvider to set web proxy.
The sample code as follows:

    HtmlLoadOptions options = new HtmlLoadOptions();
    options.StreamProvider = new StreamProvider47584(proxyurl);
    Workbook wb = new Workbook(filePath + "HtmlPage2.html", options);                
    wb.Save(filePath + "Page_out.xlsx");


    class StreamProvider47584 : IStreamProvider
    {
        private string proxyUrl;
        public StreamProvider47584(string url)
        {
            this.proxyUrl = url;
        }

        void IStreamProvider.InitStream(StreamProviderOptions options)
        {
            WebProxy proxy = new WebProxy(proxyUrl);
            WebRequest request = WebRequest.Create(options.DefaultPath);
            request.Proxy = proxy;
            WebResponse response = request.GetResponse();
            options.Stream = response.GetResponseStream();             
        }

        void IStreamProvider.CloseStream(StreamProviderOptions options)
        {
            if (options != null && options.Stream != null)
            {
                options.Stream.Close();
            }
        }           
    }

Can you try it and let us know your feedback.

Hi Ahsan Iqbal,
I tried the sample code which you have provided, it did not work. I am not able to see the images in output excel file.
Thank you.

@kumar.penigalapati,
Can you debug your code to find whether getting image stream? Getting image stream is in the StreamProvider47584 class.
You can change the method “InitStream(StreamProviderOptions options)” for getting image stream.

Hi Ahsan Iqbal,
yes i am getting the stream fine under StreamProvider47584 class InitStream method.
when i have 3 images in the html file i am getting all 3 streams fine.
but still i don’t see the images in excel output file.

@kumar.penigalapati,
Thanks for the feedback. We will share our feedback soon.

@kumar.penigalapati,

Please run the following code in your web application:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
ServicePointManager.SecurityProtocol |= (SecurityProtocolType)0x300;
ServicePointManager.SecurityProtocol |= (SecurityProtocolType)0xC00;

WebProxy proxy = new WebProxy(proxyUrl);
WebRequest request = WebRequest.Create("https://csmusdev.servicenowservices.com/10Things02a.pngx");
request.Proxy = proxy;
WebResponse response = request.GetResponse();
Bitmap.FromStream(response.GetResponseStream(), true).Save(filePath + "out.pngx");

Can you get the image?

Hi Amjad Sahi,
yes I am able to see the image fine.
out.zip (17.9 KB)

@kumar.penigalapati,
Thank you for sharing the feedback. We are analyzing it and will share our feedback soon.

@kumar.penigalapati,
Please run the following code in your web application:

        HtmlLoadOptions options = new HtmlLoadOptions();
        options.StreamProvider = new StreamProvider47584(proxyurl);
        Workbook wb = new Workbook(filePath + "HtmlPage2.html", options);                
        wb.Save(filePath + "Page_out.xlsx");


class StreamProvider47584 : IStreamProvider
{
    private string proxyUrl;
    public StreamProvider47584(string url)
    {
        this.proxyUrl = url;
    }

    void IStreamProvider.InitStream(StreamProviderOptions options)
    {
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
        ServicePointManager.SecurityProtocol |= (SecurityProtocolType)0x300;
        ServicePointManager.SecurityProtocol |= (SecurityProtocolType)0xC00;

        WebProxy proxy = new WebProxy(proxyUrl);
        WebRequest request = WebRequest.Create(options.DefaultPath);
        request.Proxy = proxy;
        WebResponse response = request.GetResponse();
        Stream srcStream = response.GetResponseStream();

        byte[] imageBytes = CopyStreamToByteArray(srcStream);
        options.Stream = new MemoryStream(imageBytes);           
    }

    void IStreamProvider.CloseStream(StreamProviderOptions options)
    {
        if (options != null && options.Stream != null)
        {
            options.Stream.Close();
        }
    }           
}

internal static byte[] CopyStreamToByteArray(Stream stream)
{
    int length = (int)stream.Length;
    byte[] data = null;
    if (length == 0)
    {
        if (stream.CanSeek)
        {
            stream.Position = 0;
        }
        data = new byte[4096];
        MemoryStream dest = new MemoryStream();
        while (true)
        {
            int size = stream.Read(data, 0, data.Length);
            if (size > 0)
            {
                dest.Write(data, 0, size);
            }
            else
            {
                break;
            }
        }
        return dest.ToArray();
    }
    else
    {
        data = new byte[length];
        if (stream.CanSeek)
        {
            stream.Position = 0;
        }
        stream.Read(data, 0, length);
    }

    return data;
}

Can you try it and let us know your feedback.

@kumar.penigalapati,

Moreover, If you still could not get the image, please check the imageBytes of every image stream and provide us more details on it, we will check it soon.

Hi Amjad Sahi,
with the example you provided i was getting an error at the line
int length = (int)stream.Length;

but i have modified it little but and after that it works fine, i am able to see the images fine.
but the only issue is they are not aligned correctly, they are on top of each other.
please see the attached code.WebApplication7.zip (8.3 MB)

@kumar.penigalapati,
Please spare us little time to investigate it and share our feedback.

Hi Ahsan Iqbal,
Any update ? we have a release approaching in a week, we would like to get this thing working as soon as possible.
Appreciate your efforts if you can quickly resolve this.
Thank you.

@kumar.penigalapati,
We are gathering information about this issue and will share our feedback as soon as available for sharing.