The display ratio of the image inserted into Excel in the C#eplus library is broken, so I want to fix it.

Asked 2 years ago, Updated 2 years ago, 51 views

C# provides a template (.xlsx) on the server side and
from the client (html) We are creating a program to display the download dialog with the values requested in post.

I'm using "epplus 4.1.0" for my library.
If you implement it like the code below, the display size and ratio of the png image inserted into Excel will be broken.The image to be inserted is 100px by 100px.
[Code]

byte[]source;
 MemoryStream stream=new MemoryStream();
 using(vartemplate=System.IO.File.OpenRead(@/template.xlsx))
 using(var excel=new ExcelPakage(stream, template))
 {
 ExcelWorkSheetworkSheet=excel.Workbook.Works.Where(s=>s.Name 
 =="Sheet1").FirstOrDefault(); 
 System.Drawing.Image img=System.Drawing.Image.FromFile(@/logo.png);

 using(var picture=worksheet.Drawings.AddPicture("image", img)){
 int imageHeight=picture.Image.Height;//Verify that 100 has been obtained here
 int imageWidth=picture.Image.Width;//Verify that 100 has been obtained here
 picture.setSize(100)
 }

 package.Save();
 }    
 source=stream.ToArray();
 return source;

[Results]
If you look at the image of the downloaded Excel file in Excel 2016>Formatting Graphics>Size,
Height: 3.18cm Width: 2.82cm Height magnification: 150% Horizontal magnification: 133% Fixed aspect ratio:with check
Based on original size—Checked.
I don't know if it's related, but the screen resolution was 120 DPI.
Press the Reset button to return to the original correct image size.

[Question]
①If anyone knows the cause or solution to the above, could you please let me know?

e I'm not particular about epplus, so I'll do something else
Is there a way to make the download dialog appear to the client after editing Excel on the server (avoiding the above problem)?I use epplus because I can create byte[] (I tried COM, but I didn't know how to reply to download Excel objects.)

(Added 20170920)
Dear pgrho,

1. I was able to set the DPI setting to 96dpi according to the method you mentioned.
  The image ratio remained broken.

2.
for cells to insert images as advised   After setting the width and width to fit the image in advance,
  I was able to display the display ratio in full size.

[Example Code]
sheet.Column(1).Width=100
sheet.Row(1).Height=100

So COM is not allowed to be used on the server...I didn't study enough.
I would like to discuss how to adjust the cell size of the template using eoplus.
Thank you for your reply.

c# excel

2022-09-29 21:52

1 Answers

The standard DPI for Windows is 96, so 120 dpi if the display in the running environment is set to 125% magnification.It is probably different from the expected value as of img.VerticalResolution or HorizontalResolution, so you can change it with the Bitmap.SetResolution method.

varimg=(Bitmap) Image.FromFile(@"logo.png");
img.SetResolution (96,96);

However, the above only changes the magnification rate from 96dpi to 100%, so I don't think the display will change.

Excel's Y coordinate system is special and is represented by a combination of rows and positions in rows, not absolute coordinates.There must have been errors and calculation errors during this conversion.Changing the size of the line to which you want to paste may improve.

Also, using Excel COM objects in web applications such as ASP.NET is considered a license violation and should be avoided.Also note that System.Drawing.dll is not supported.


2022-09-29 21:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.