While converting Range into Image is giving wrong image

When I’m converting particular range using ImageOrPrintOptions I’m facing there is some extra data is appear in it.
Here is my code snippets and file for reference.

Workbook workbook = new Workbook("C:\\Users\\ABC\\Desktop\\Samons chart issue\\Test.xlsx");
Aspose.Cells.Worksheet worksheet = workbook.Worksheets["SIND"];

worksheet.PageSetup.ClearHeaderFooter();
worksheet.PageSetup.PrintArea = "S12:X20";
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.OnePagePerSheet = true;
options.ImageType = ImageType.Emf;
options.HorizontalResolution = 300;
options.VerticalResolution = 300;
SheetRender sr = new SheetRender(worksheet, options);
byte[] bytes = null;
using (MemoryStream st = new MemoryStream())
{
    sr.ToImage(0, st);
    bytes = st.ToArray();
}
//item.Text = Convert.ToBase64String(bytes);
File.WriteAllBytes("C:\\Users\\ABC\\Desktop\\Samons chart issue\\23.emf", bytes);

Here is my excel file
Test.zip (52.2 KB)
Here is my expected output and Error output.
Output.zip (26.5 KB)

@Jayshiv,

Thanks for the template Excel file.

I evaluated your scenario/case using your template Excel file and code snippet. I checked your template Excel file and found the option “Rows to repeat at top” in “Page Setup|Sheet” tab is set to “$3:$9”. That’s why when you render the range to image, those rows data is rendered at the top as well. You need to remove the value for the option. Please add a line (in bold) to your code segment. I have tested by adding the line and it works fine and as expected.


worksheet.PageSetup.ClearHeaderFooter();
worksheet.PageSetup.PrintArea = “S12:X20”;
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;
worksheet.PageSetup.PrintTitleRows = “”;//disable the option
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.OnePagePerSheet = true;
options.ImageType = ImageType.Emf;
options.HorizontalResolution = 300;
options.VerticalResolution = 300;

Let us know if you still find any issue.

1 Like

@amjad.sahi.It’s work Properly. Thanks for your quick response and guidance.

@Jayshiv,

You’re welcome! I’m glad to hear that it meets your needs. If you have any more questions or comments, please don’t hesitate to reach out to us.