Spire.XLS for .NET is a professional Excel .NET library that can be used to any type of .NET( C#, VB.NET, ASP.NET, .NET Core) application, both ASP.NET web sites and Windows Forms application.

Fri Aug 17, 2018 2:56 am

Support,

I am using spire.office evaluation version and I am trying to open an existing excel convert the specific range of cells to Image. But the text in the image is very blurry and of low resolution. I tried to follow the steps in the below link e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Conversion/Convert-Excel-Sheet-to-a-High-Resolution-Image-in-C-VB.NET.html . However, the resolution has not been improved and with the proposed solution the image is showing up on complete white background . I am trying to achieve an image of good quality also the image should look like copy picture from excel and pasting it as PNG. Please advise.



Below is my code:

//actual code for PNG

ws = workBook.Worksheets["sheet1"];

ws.SaveToImage(tempFolder+"test.png",1, 6, 26, 12);

//suggested code in the link

MemoryStream ms = new MemoryStream();

ws.ToEMFStream(ms, 1, 1, 26, 12);

Image image = Image.FromStream(ms);

Bitmap images = ResetResolution(image as Metafile, 300);

images .Save(tempFolder + "sheet1", ImageFormat.Png);

pran
 
Posts: 2
Joined: Thu Aug 16, 2018 4:28 pm

Fri Aug 17, 2018 4:17 am

Hi,

Thanks for your inquiry.
From your information, you should be using an old version since the method WorkSheet.SaveToImage was obsolete(the new method ToImage instead) in latest version. I first suggest using our latest Spire.Office Platinum (DLL Only) Version:3.8.0 which is more stable. If you still have the issue, please share following information for further investigation.
1.The input Excel and result image you got
2.The full code if it is different from the code you just provided
3.The OS and Region information, e.g. Win7 64bit, China/Chinese.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 2940
Joined: Tue Sep 06, 2016 8:30 am

Sat Aug 18, 2018 5:09 am

Thanks for the response. When i try to use toimage method it only accepts four arguments, among them i dont find the filepath or filename. how do i provide where the file should be saved?

pran
 
Posts: 2
Joined: Thu Aug 16, 2018 4:28 pm

Mon Aug 20, 2018 7:54 am

Dear pran,

Sorry for late reply as weekend and thanks for your detailed information via email.
I found the "border" you want was the gridlines in Excel.
gridlines.jpg
gridlines.jpg (34.45 KiB) Viewed 894 times

When converting to image, the gridlines will not be saved by default. So please add following code before saving to image.
Code: Select all
worksheet.PageSetup.IsPrintGridlines = true;

Note the gridline is actually dashed, if you want a solid line in image, you have to set the border with solid line via code. As for the resolution, please use the code in the guide(Convert Excel Sheet to a High-Resolution Image). Below is sample code for your reference. I attached the result files in the email, please have a check and let us know if this can meet your requirements.
Code: Select all
            Workbook book = new Workbook();
            book.LoadFromFile(@"F:\testing\xls test form\sample document\TestFile14700.xlsx");
            Worksheet ws = book.Worksheets["New headline"];
            CellRange cr = ws.Range[1, 6, 26, 12];
            //set the border
            cr.BorderInside(LineStyleType.Thin, Color.DarkGray);
            cr.BorderAround(LineStyleType.Thin, Color.DarkGray);
            //set the margin
            ws.PageSetup.LeftMargin = 0;
            ws.PageSetup.BottomMargin = 0;
            ws.PageSetup.TopMargin = 0;
            ws.PageSetup.RightMargin = 0;
            //ws.PageSetup.IsPrintGridlines = true;
            Image image = book.Worksheets["New headline"].ToImage(1, 6, 26, 12);
            //original image
            image.Save(@"14700-original.png");
            using (MemoryStream ms = new MemoryStream())
            {
                ws.ToEMFStream(ms, 1, 6, 26, 12);
                Image imageRe = Image.FromStream(ms);
                Bitmap imageRR = ResetResolution(imageRe as Metafile, 300);
                imageRR.Save("14700-ResetResolution.jpg", ImageFormat.Jpeg);
            }
        }
        private static Bitmap ResetResolution(Metafile mf, float resolution)
        {
            int width = (int)(mf.Width * resolution / mf.HorizontalResolution);
            int height = (int)(mf.Height * resolution / mf.VerticalResolution);
            Bitmap bmp = new Bitmap(width, height);
            bmp.SetResolution(resolution, resolution);
            Graphics g = Graphics.FromImage(bmp);
            g.DrawImage(mf, 0, 0);
            g.Dispose();
            return bmp;
        }



Best wishes,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 2940
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.XLS