Image from memory stream: Invalid image scales

Hi.


I’m loading our company logo from SQL Server and inserting it into Excel worksheets. Image is loaded and scaled in code:

Dim ms As New IO.MemoryStream
Dim coeff As Single

'This is to keep aspect ratio
coeff = 200.0 / CSng(apr.Company.Logo.Width)
If (coeff * apr.Company.Logo.Height) > 200 Then coeff = 200.0 / CSng(apr.Company.Logo.Height)

'Resize image to max 200 pixels wide and max 200 pixels high
Dim bm As New System.Drawing.Bitmap(apr.Company.Logo, CInt(coeff * apr.Company.Logo.Width), CInt(coeff * apr.Company.Logo.Height))

'PNG, because of transparency
bm.Save(ms, System.Drawing.Imaging.ImageFormat.Png)

Then inserted into worksheet:

ws.Pictures.Add(0, 0, ms)
ws.Pictures(0).IsLockAspectRatio = False
ws.Pictures(0).IsAutoSize = False
ws.Pictures(0).Width = bm.Width
ws.Pictures(0).Height = bm.Height

But I receive attached Excel file, in which all images are scaled to 142% height and 100% width. When I reset image in Excel, it goes to its original state which I need. What’s wrong?! For clarification: I resize image in code because of performance issues rising with IIS (many gigabytes of memory usage and server crash). Version info: Cells 8.6.0.5 on VS 2013 (.NET 4.5)

Thanks.

I tried to re-produce issue in a simple windows application to post here, but found that when I create an empty excel file, this problem doesn’t occur. When I pass excel file attached to first post as input, problem re-appears and causes all further-inserted images to distort too. Sample input file, logo image file, generated output file and a sample output for clean input are attached. Distortion can be seen on first sheet of file. Code is extremely simple:


Dim wb As New Cells.Workbook(“C:\Input.xlsx”)

wb.Worksheets(0).Pictures.Add(10, 10, “C:\Logo_9.png”)
wb.Worksheets(0).Pictures.Add(10, 15, “C:\Logo_9.png”)
wb.Save(“C:\Output.xlsx”, Cells.SaveFormat.Xlsx)

Seems this is a problem related to generated Excel file, but what?!

Hi Mohammad,


Thank you for contacting Aspose support.

We have evaluated the presented scenario while using the latest version of Aspose.Cells for .NET 8.6.0.5, and we are able to replicate the said problem (increased image height scale in spreadsheet) when we used your sample Re-production Input.xlsx as input to the process. However, the issue didn’t replicate when the input is Output for clean Input.xlsx. We have logged these observations for the product team’s review under the ticket CELLSNET-43999. Our product team will further look into the details of this problem and we will keep you updated on the status of correction.

We apologize for your inconvenience.

Many thanks for your response. I’m waiting for a fix or guideline on this issue.


Regards

Hi,

We appreciate your patience and good words and using Aspose.

We will try our best to share a good news with you. Hopefully, your issue will take a week or so. In case, the issue is difficult to be resolved or we have some other priority tasks, then it might take couple of weeks. We will keep you updated with any progress relating to this issue.

Hi,


Thanks for using Aspose.Cells.

We have looked into this issue further and found that this issue is caused by that the row height is automatically fitted and the stored row height is not correct in the file.


Please call Worksheet.AutoFitRows method to fit row height .


Please see following code for your reference.

C#
Workbook wb = new Workbook(path + “Input.xlsx”);
AutoFitterOptions options = new AutoFitterOptions();
options.OnlyAuto = true;

wb.Worksheets[0].AutoFitRows(options);
Console.WriteLine(wb.Worksheets[0].Cells.GetRowHeightPixel(10));
wb.Worksheets[0].Pictures.Add(10, 10, path + “Logo.png”);
wb.Worksheets[0].Pictures.Add(10, 15, path + “Logo.png”);
wb.Save(path + “dest.xlsx”);

Hi


Thanks for your guide. I did this (part of code):

Dim afr As New Aspose.Cells.AutoFitterOptions

afr.OnlyAuto = True
ws.AutoFitRows(afr)
ws.Pictures.Add(0, 0, String.Format("{0}\Logo_{1}.png", Server.MapPath("~/IOFiles"), apr.Company.ID))

If this seems correct to you, I should say that this didn’t solve my problem. Still I get the same output attached to first post. Any more suggestions? Please note that I don’t use a template file. Everything is built from scratch and in the same code (no intermediates).

EDIT: I tested and found that height of row 0 is 17 pixels before autofit call (using GetRowHeightPixel(0)). How does this cause image to be stretched?

Regards.

Hi,

Thanks for your feedback and using Aspose.Cells.

Please provide us your simple and complete runnable sample code in the form of console application project or a single webform ASP.NET application which we could run at our end and replicate this issue. We will investigate it further and update you asap. Thanks for your cooperation.

Attached to this reply, you can find a solution containing two projects, one of which is a windows application containing full code generating Excel file. The other project is a class library needed for windows application. A database (Access 2013, containing all real-case data) and logo file are also attached. All mentioned files are compressed as RAR. I tested this on my machine and could reproduce issue.


Regards.

Hi Mohammad,

Thanks for providing us nice application and using Aspose.Cells.

However, your application is complex but we are hopeful that we will manage to grasp it with some effort. But we were expecting very simple application where only picture is added and shows the problem at hand i.e image from memory stream gets scaled up.

I was successfully able to run your application after changing the paths of Save() method, Pictures.Add() method and perfapp.accdb file and generated the output attached with this post.

We need your more favor. Please download the attached file and provide us the screenshot highlighting the issue with red circles and it will also be good if you could provide us the expected output excel file which you can create manually using Microsoft Excel.

We will then investigate this issue further and submit it to product team for a fix. We appreciate your cooperation in this regard and hope your issue will be fixed at the earliest.

Hi Shakeel


At first, many thanks for your consideration and effort on running my application. I understand your need for a simple code, but as you see, problem doesn’t rise in a simple Excel output. So I needed to provide you with real case output so you can re-produce issue at your side. To do this, you needed the complex code which can create complex output.

Second, as you see in code, I have switched from direct memory stream approach to an intermediate image file. Image file is directly addressed in Pictures.Add call so there is no doubt in source of problem (which can rise from scaling method in memory stream approach or bitmap objects in .NET architecture).

Finally, as you see, your provided output is problematic as mine is. Logo is stretched vertically. I have attached the screenshots you asked. One shows the problem and second shows what happens when I reset picture in Microsoft Excel. I need exactly what results from picture resetting. You can also find expected output file which I made from problematic output file just by resetting images.

Regards

Hi,

Thanks for your screenshots, comments, explaining the issue in detail and using Aspose.Cells.

When I comment your most of the code, then issue does not occur, so there is some problem inside your code which needs to be fixed. I even tried the previously suggested solution of AutoFitterOptions but it did not have any effect.

We have therefore logged this issue in our database for investigation. We will look into it and fix the issue or suggest you the correct code. Once, there is some news for you, we will let you know asap.

This issue has been logged as

  • CELLSNET-44002 - Image gets scaled because of some problem in the code

Thanks for your time and detailed explanations. I’m waiting for future updates and greatly appreciate any suggestion on fixing this problem.


Regards.

Hi,

Thank you. Once, we will have some update for you, we will let you know and we are hopeful your issue will be fixed soon and will get a chance to share a good news with you. Have a good day.

Hi


Thanks, I’m waiting for good news. As a complement to given data, I did another test a few minutes ago. After saving workbook, I reopened it in the same code and evaluated HeightScale of all inserted images using this method:

Dim wb2 As New Aspose.Cells.Workbook(“C:\Output.xlsx”)

For i = 0 To wb2.Worksheets.Count - 2
MsgBox(wb2.Worksheets(i).Pictures(0).HeightScale)
Next

I received 100 for all images! This also happens before saving workbook. I think there is a problem between Aspose.Cells and Excel which causes Excel to interpret given height scale by Aspose.Cells in an incorrect way, resulting in numbers above 100.

Regards.

Hi,


Thank you for sharing your valuable insight on this matter. We have logged your observations to the ticket CELLSNET-44002 for further analysis. As soon as we get more information in this regard, we will post here for your kind reference.

Hi,

Thanks for using Aspose.Cells.

This is to inform you that we have fixed your issue CELLSNET-44002 now. We will soon provide the fix after performing QA and including other enhancements and fixes.

Hi.


First, sorry for late reply. I didn’t check my email in past two days.
Second, many many thanks for your effort on solving this problem. I appreciate it and am waiting for the fix.

Regards.

Hi,

Thanks for your using Aspose.Cells.

Please download and try the latest fix: Aspose.Cells for .NET v8.6.1.2 and let us know your feedback.

Hi.


I don’t know why, but I didn’t receive any email for your post! Anyway, many thanks for your effort, but this didn’t solve my problem. If you are successful in solving my problem with this version and sample application I provided previously, please let me know. Maybe I am wrong in someway and doing something incorrectly.

Regards.